Hacker Newsnew | past | comments | ask | show | jobs | submit | webstersx's commentslogin

Although I don't know exactly what the implications are just yet, I've created a category on one of my RLMObject subclasses which accepts an NSArray or NSDictionary property and in the getter/setter converts to and from the NSData backing property. The only thing is after [Venue creatInDefaultRealmWithObject:] I have to explicity set the value for it to propogate to the backing NSData property.

It's extra work, but it lets me manipulate Realm into doing what I want it to do to store data which doesn't conform to an explicit schema. (e.g. a Venue which has an array of address strings, where I don't want the headache of iterating through venue.address[i].string values:

(note: this doesn't look pretty, its doing some funky formatting!)

"venue" : { "name":"Test Venue", "address" : [ "address line 1", "address line 2", "address line 3 for rare instances" ] }

@interface Venue : RLMObject @property NSString name; @property NSData addressData; @end

@interface Venue (AddressToData) @property NSArray address; @end

+ (NSDictionary )defaultPropertyValues { return @{ @"addressData":[NSJSONSerialization dataWithJSONObject:@[] options:0 error:nil] }; }

- (NSArray) address { NSArray address = nil;

    if (self.addressData) {
        address = [NSJSONSerialization JSONObjectWithData:self.addressData options:0 error:nil];
    } else {
        address = @[];
    }
        
    return address;
}

- (void) setAddress:(NSArray )address { if (!address) { address = @[]; }

    NSData *data = [NSJSONSerialization dataWithJSONObject:address options:0 error:nil];
    
    self.addressData = data;
}


    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm beginWriteTransaction];
    
    for (NSDictionary *v in json[@"venues"]) {
        Venue *venue = [Venue createInDefaultRealmWithObject:v];
        NSArray *address = v[@"address"];
        venue.address = address;
    }
    
    [realm commitWriteTransaction];


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: