0

I am using Realm 0.92.3 but it crashed when I have null value despite I have set the default properties. Is there any solution on this? If not I might convert using coredata as this is very important to me. The null will be random on several properties

@interface WatchlistNews : RLMObject
@property NSString              *nid;
@property NSString              *tid;
@property NSString              *country;

@end


 @implementation WatchlistNews
 + (NSString *)primaryKey {
     return @"nid";
 }

 + (NSDictionary *)defaultPropertyValues {
     return @{@"nid" : @"", @"tid": @"", @"country": @""};
 }
 @end

Data response:

nid = 509319;
tid = <null>;
country = my;

Error code:

 -[NSNull UTF8String]: unrecognized selector sent to instance 0x10712b4c0 
 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull UTF8String]: unrecognized selector sent to instance 0x10712b4c0'
shoujo_sm
  • 3,173
  • 4
  • 37
  • 60
  • Please post the stacktrace. It's not obvious from the code you posted where the issue is. – trojanfoe May 19 '15 at 10:48
  • @trojanfoe not sure what you meant by stacktrace but I have updated my questions with more details – shoujo_sm May 19 '15 at 13:32
  • OK so an `NSNull` object is being used in place of an `NSString` object. Your code shows no use of `NSNull` objects. You need to be checking what objects are before working on them. – trojanfoe May 19 '15 at 13:33

2 Answers2

0

Realm do not support complex data types, so if you try to assign a complex value such as <null>, it gets crashed.

You should check the response you are getting from the server for the <null> values. And if it exists in the response replace it with an empty string. Try following code on the response you are getting, to remove the occurance of <null> values.

-(NSMutableDictionary *) removeNullFromDictionary:(NSDictionary *) originalDictionary{

NSArray *allKeysArray = [originalDictionary allKeys];
const NSMutableDictionary *replaced = [originalDictionary mutableCopy];
const id nul = [NSNull null];
const NSString *blank = @"";

for(NSString *key in allKeysArray) {

    const id object = [originalDictionary objectForKey:key];

    if(object == nul) {

        [replaced setObject:blank forKey:key];

    }
}
return [replaced copy];

}

Pradeep Singh
  • 752
  • 1
  • 10
  • 23
0

Realm does not yet support setting nil for NSString properties, but you can track progress on that by following https://github.com/realm/realm-cocoa/issues/628.

segiddins
  • 4,110
  • 1
  • 17
  • 22