from what I see if the value is nil for a property it always throws exception. sometimes i cannot simply set a '' empty string as nil, so what's the solution in realm? thanks.
Null is now fully supported.
OLD answer:
Supporting nil/NULL is on the roadmap.
Until then there are two workarounds:
Add a separate property to indicate if your property is nil.
#interface IntObject : RLMObject
#property NSInteger myProp;
#property boolean myPropIsNil;
#end
Wrap your property in an object:
Object properties (links), can be nil though. So if you need a nullable int property, for example, you can wrap it in a new Realm model, like this:
#interface IntObject : RLMObject
#property NSInteger myProp;
#end
Then anytime you want to have an optional "int" property in your models, you can write this:
#interface MyModel : RLMObject
#property IntObject *optionalMyProp;
#end
Related
CONTEXT
Realm does not support indices on relationship properties (objects). https://realm.io/docs/objc/latest/#indexed-properties
If you try, it'll throw an error.
We have a situation, where we need to query a model's relationship and another property.
Typically you would do this by having a covering index across (foreign_id, property), but this does not appear to be possible in Realm (yet?)
For example
#interface Book : RLMObject
#property NSNumber<RLMInt> * page;
#end
#interface Page : RLMObject
#property Book * book;
#property NSNumber<RLMInt> * line;
#end
[Page objectsInRealm:realm where:#"book.uuid = %# AND page.line = %#", uuid, #1];
QUESTION
What is the best way to set up indices so that the query above is optimal?
Are relationships already indexed?
Or Do I create another property on Page, called book_uuid, and index on that?
Cheers
Realm doesn't really cater to the foreign key mechanism of relational databases, so it's easier to get out of that mindset when designing your data model.
In this case, I think it would be more appropriate that your Book model stores an array of all of your pages (sort of an inverse to the foreign key concept), which you can use to initially filter the pages to just that book, and to then query for the specific page line:
#interface Page : RLMObject
#property NSInteger line;
#end
RLM_ARRAY_TYPE(Page)
#interface Book : RLMObject
#property RLMArray<Page *><Page> *pages;
#end
Book *book = [[Book allObjects] firstObject];
Page *page = [[book.pages objectsWhere:#"line = %#", #1] firstObject];
As long as you've marked line as an indexed property, this should work very quickly in theory. But since you've mentioned you've got thousands of page lines, I'd be curious to see what the real-world performance of this would be.
Cannot figure out why I get this error.
No visible #interface for 'RLMObject' declares the selector 'createOrUpdateInRealm:withValue:'
I have included the Realm/Realm.h header
Define my RLMObject in this manner
Class aClass = NSClassFromString(modelName);
RLMObject *m = [[aClass alloc] init];
Then I create a NSMutableDictionary to contain values which I want to partially update on the RLMObject.
NSMutableDictionary *updateValues = [[NSMutableDictionary alloc] init];
And then I call createOrUpdateInRealm:withValue: on m
[m createOrUpdateInRealm:realm withValue:updateValues];
But I get the error. I have no idea why this happens?
createOrUpdateInRealm:withValue: should be called on your subclass, not on an instance.
You should use it like this:
CustomObject *myCustomObject = [CustomObject createOrUpdateInRealm:realm withValue:dictionary];
where CustomObject is a subclass of RLMObject.
+createOrUpdateInRealm:withValue: is a class method of RLMObject, not an instance method. This means you need to call it on your subclass directly, not on an instance of your subclass:
[MyClass createOrUpdateInRealm:realm withValue:dictionary];
I have a WKInterfaceController and I added a table as following:
// .h
#interface InterfaceController : WKInterfaceController
#property (weak, nonatomic) IBOutlet WKInterfaceTable *table;
#end
// .m
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"did select");
}
- (void)table:(WKInterfaceTable *)table
didSelectRowAtIndex:(NSInteger)rowIndex{
NSLog(#"did select new");
}
However neither of the two methods gets called. I was unable to find any protocol declaration for WKInterfaceTable and neither any delegate property on the table.
Is there something that I am missing here?
I found out that the method was never called because I had set a segue to be triggered on selection of the row on Interface builder.
It seems that that by having no delegation and table protocols once you set a delegate it stops the didSelectRow method from being called.
In Apple's WKInterfaceController document it states that if you do not have any actions or segues then the method called is:
- table:didSelectRowAtIndex:
If you use segues then the methods called are:
For buttons:
- contextForSegueWithIdentifier:
For tables:
- contextForSegueWithIdentifier:inTable:rowIndex:
Swift 4
Here’s an example of selecting a WKInterfacetable row in a REST/JSON implementation.
Create a context property instance of the array class instead of using self.pushController.
override func table(_ table: WKInterfaceTable, didSelectRowAt rowIndex: Int) {
let message = messageObjects[rowIndex]
presentController(withName: "MessageView", context: message)
}
I use KVC in my projects.
And, in one of my classes, I wrote the property :
#property ( nonatomic, strong ) NSString *notes;
I want to put a NSString object in that property :
And before setting the value, I want to test the class name of the destination.
a = [ newContainer valueForKey:#"notes"];
if( a != nil && ![ b isKindOfClass:[ a class ] ] )
// here b is the new NSString value
The result is that xcode indicates that the classes aren't the same !
(gdb) po [ b class ]
__NSCFString
(gdb) po [ a class ]
__NSCFConstantString
I read that is not very important and that __NSCFConstantString is a private subclass of NSString.
But, in my case, I need to check all properties of my object before updating it.
And I don't want to had in my code :
// OK, classes aren't the same ...
// ---- BUT WE MUST test it again to know if a is a NSString and b a subclass of NSString or anything else ...
beurk !
Is anyone have the same problem ?
Thanks a lot for your help !
Three points:
1- You should check against [NSString class], the publicly exposed class of your property, not against the class of the current value of your property.
Imagine what happens when you check against the value class, instead of the property class: after setting your property to a NSMutableString, which is a perfect instance of NSString, you could not any longer set it to a regular NSString (since NSString is not a subclass of NSMutableString). Your current problem is a variant of the one described in this paragraph, which may be easier to understand.
2- The test should be done in the class that owns the property, not outside as you are doing now. Because only that class is entitled to know about the type of object it accepts.
3- So. Use the standard validateValue:forKey:error: method, which is your friend here. This method would be implemented by the class, and it would make sure notes is a NSString. Outside of the class, you would not check the type of the value directly, but ask the container class to validate it.
And voilà !
Using WSDL2ObjC I am getting lot of classes which are subclasses of NSString.
I am finding it troublesome to initialize the NSString value of any object of those classes.
Say my class looks like this :
#interface mClass ; NSString {
int value;
}
Now in my code I would like to use objects of mClass as both NSString and also want to use its attribute value which is an integer.
How can I do that?
I am trying to use code like this
mClass *obj = [[mClass alloc] initWithString:#"Hello"];
But it's showing me an error saying I am using an abstract object of a class , I should use concrete instance instead.
If you really need make NSString subclass you should override 3 methods:
- (instancetype)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer;
- (NSUInteger)length;
- (unichar)characterAtIndex:(NSUInteger)index;
For example:
MyString.h
#interface MyString : NSString
#property (nonatomic, strong) id myProperty;
#end
MyString.m
#interface MyString ()
#property (nonatomic, strong) NSString *stringHolder;
#end
#implemenation MyString
- (instancetype)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer {
self = [super init];
if (self) {
self.stringHolder = [[NSString alloc] initWithCharactersNoCopy:characters length:length freeWhenDone:freeBuffer];
}
return self;
}
- (NSUInteger)length {
return self.stringHolder.length;
}
- (unichar)characterAtIndex:(NSUInteger)index {
return [self.stringHolder characterAtIndex:index];
}
#end
It might be smarter to use a wrapper class that internally uses NSStrings to do whatever operations or manipulations you are trying to do. However this will cause you to need to overload any functionality of NSString you want (such as getting the length of the string).
Or, you could create a category of NSString (found right next to Objective-C class in the new file window). This allows you to add any properties or methods that you wish to be "added" to the NSString class. Now just import this category wherever you wish to use it and you will have all of your custom functions available on any NSStrings objects.
Do you really need to subclass NSString? It’s a class cluster, which (apart from other things) means it’s hard to subclass. There’s a good post by Mike Ash on subclassing class clusters. If you didn’t know that class clusters existed you are probably new to Cocoa and in that case the best short answer is don’t try to subclass class clusters.
There’s also previous questions about subclassing NSString here on Stack Overflow. Next time you might want to search a bit before asking a new question.