objective c - Update Realm database -
i'm trying update realm database, can't figure out.
i using [realm addobject:info];
, add same objects realm database existed.
so replaced [people createorupdateinrealm:realm withvalue:info];
added last item in array of people information (there 6 people, realm database show sixth person information).
not sure i'm doing wrong?
people.h:
@property (nonatomic) nsstring *fname; @property (nonatomic) nsstring *lname; @property (nonatomic) nsstring *flname; @property (nonatomic) nsstring *email; @property (nonatomic) nsstring *phone; @property (nonatomic) nsstring *video; @property (nonatomic) nsstring *pdf; @property (nonatomic) nsstring *pkey; + (nsstring *)primarykey;
people.m:
+ (nsstring *)primarykey { return @"pkey"; }
tableviewcontroller.m:
rlmrealm *realm = [rlmrealm defaultrealm]; (id item in responsearray) { [realm beginwritetransaction]; people *info = [[people alloc] init]; info.fname = item[@"fname"]; info.lname = item[@"lname"]; info.flname = [nsstring stringwithformat:@"%@ %@", item[@"fname"], item[@"lname"]]; info.phone = item[@"phone"]; info.video = item[@"video"]; info.pdf = item[@"pdf"]; [people createorupdateinrealm:realm withvalue:info]; [realm commitwritetransaction]; }
the responsearray
comes data api.
you don't provide definition of +primarykey
method, suspicion pkey
property primary key. you're not setting pkey
property on info
object create, results in being left @ default value of nil
. means +createorupdateinrealm:withvalue:
sees asking update same object each time through loop: object primary key of nil
.
setting pkey
property on info
before calling +createorupdateinrealm:withvalue:
should result in of objects being saved expect.
note it's preferable minimize number of write transactions, each write transaction has amount of overhead. in case can move write transaction outside of loop.
Comments
Post a Comment