我試圖更新領域數據庫,但無法弄清楚。更新領域數據庫
我正在使用[realm addObject:info];
,但這只是將相同的對象添加到已存在的Realm數據庫中。
然後我用[People createOrUpdateInRealm:realm withValue:info];
替換了那個,但是隻添加了我的人信息數組中的最後一項(有6個人,但Realm數據庫只顯示第六人信息)。
不知道我在做什麼錯?
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];
for (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];
}
的responseArray
來自API的數據。
你放了RLMRealm * realm = [RLMRealm defaultRealm];在循環中,所以每次都會寫完。把它放在循環之外 –
@TejaNandamuri我將更新我的代碼。出於某種原因,我仍然遇到同樣的問題? – SRMR
將調用放置到'[RLMRealm defaultRealm]'與您看到的任何問題無關。 – bdash