CoreData更新无法使用MagicalRecord

时间:2015-10-30 18:12:21

标签: objective-c core-data magicalrecord-2.2

我正在尝试使用MagicalRecord(版本2.3)更新现有的CoreData实体;这是我的代码:

PreferenceData *updateData = [PreferenceData MR_createEntityInContext:defaultContext];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"aHourFormat == %@ || aHourFormat == %@", [NSNumber numberWithInt: 12], [NSNumber numberWithInt: 24]];
updateData = [PreferenceData MR_findFirstWithPredicate: predicate inContext: defaultContext];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *staffNamesArray = [[userDefaults arrayForKey:@"staffNamesArray"] mutableCopy];
NSMutableDictionary *preferencesDict = [[userDefaults dictionaryForKey:@"preferencesDictionary"] mutableCopy];

NSMutableArray *addlSvcsArray = [self createAddlSvcsTextboxArray];
NSMutableArray *customServicesArray = [[userDefaults arrayForKey:@"customServicesArray"] mutableCopy];

updateData.aStaffPos1 = staffNamesArray[0];
updateData.aStaffPos2 = staffNamesArray[1];
updateData.aStaffPos3 = staffNamesArray[2];
updateData.aStaffPos4 = staffNamesArray[3];
updateData.aStaffPos5 = staffNamesArray[4];
updateData.aStaffPos6 = staffNamesArray[5];

updateData.aShopOpens = [preferencesDict objectForKey:@"shopOpens"];
updateData.aShopCloses = [preferencesDict objectForKey:@"shopCloses"];

updateData.aDeleteOldAppts = [NSNumber numberWithInteger:[oDeleteAfterXDays.text intValue]];
updateData.aHourFormat = [NSNumber numberWithInt:[preferencesDict objectForKey:@"timeFormat"]];
updateData.aServicesType = [NSNumber numberWithInt:[preferencesDict objectForKey:@"servicesType"]];
updateData.aShowApptServices = [NSNumber numberWithInteger: @0];
updateData.aColorScheme = [preferencesDict objectForKey:@"colorScheme"];
updateData.aUpdatedRecordFlag = [NSNumber numberWithInteger: @1];

updateData.aAddlSvcs1 = addlSvcsArray[0];
updateData.aAddlSvcs2 = addlSvcsArray[1];
updateData.aAddlSvcs3 = addlSvcsArray[2];
updateData.aAddlSvcs4 = addlSvcsArray[3];
updateData.aAddlSvcs5 = addlSvcsArray[4];
updateData.aAddlSvcs6 = addlSvcsArray[5];

[defaultContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
    if(success)
        NSLog(@"update successful");
    else
        NSLog(@"update failed");
}];

}

我没有错;数据在变量中,但是当我使用 sqlitebrowser 查看记录时,没有任何变化。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先在updateData中创建一个新记录,然后用搜索结果覆盖此变量。每次运行更新时,您都将创建一个只有默认值或为空的新记录。

如果您的数据未按预期更新,则您获取的updateData很可能是nil,因为谓词出现问题。