如何更新具有相同属性但值不同的核心数据对象的值?

时间:2015-02-17 05:30:07

标签: ios core-data xcode5

<SaveDbstr: 0x8b640e0> (entity: SaveDbstr; id: 0x8b64140 <x-coredata:///SaveDbstr/tC0D35B09-8B89-4233-A572-8DA6434884802> ; data: {
cityName = LAFAYETTE;
distance = "11,382.7";
driverId = 00000000097;
strainDescription = "This is no creeper! Effects are felt almost immedi";
strainId = 00000000001;
strainImage = "www.dbster.net/DbsterDocuments/StrainImages/Kandy Skunk1.jpg";
strainName = "Kandy Skunk";
strainTypeName = "Hash and Oil";
vendorBusinessName = "Raging Vaping";
vendorDriverEngagementId = 18;
vendorOverallRating = "4.3";
})
2015-02-17 09:57:56.722 CoreDatProject[570:70b] save data to core data <SaveDbstr: 0x8b042f0> (entity: SaveDbstr; id: 0x8b028b0 <x-coredata:///SaveDbstr/tC0D35B09-8B89-4233-A572-8DA6434884803> ; data: {
cityName = MENIFEE;
distance = "11,382.7";
driverId = 00000000112;
strainDescription = "This is no creeper! Effects are felt almost immedi";
strainId = 00000000001;
strainImage = "www.dbster.net/DbsterDocuments/StrainImages/Kandy Skunk1.jpg";
strainName = "Kandy Skunk";
strainTypeName = "Hash and Oil";
vendorBusinessName = Vendor1;
vendorDriverEngagementId = 20;
vendorOverallRating = "4.1";
})

使用以下代码

在核心数据中成功保存上述对象
@property (nonatomic,strong) NSManagedObjectContext *savedbstr;

savedbstr = [self managedObjectContext];
for (int i=0;i<[temp count]; i++) {
    SaveDbstr *savedoobstr = [NSEntityDescription
                                 insertNewObjectForEntityForName:@"SaveDbstr"
                                 inManagedObjectContext:self. savedbstr];
    [savedoobstr setValuesForKeysWithDictionary:[temp objectAtIndex:i]];


}

NSError *error1;
if (![self. savedbstr save:&error1]) {
    NSLog(@"Coudld not save %@",[error1 localizedDescription]);
}

如何更新具有相同属性但值不同的核心数据对象的值? 例如,我想更新第一个对象的cityName ??谢谢

1 个答案:

答案 0 :(得分:0)

您需要获取要更新的值然后更改其值并提交,而不是在此处插入不正确的新记录。 请参阅以下堆栈问题:

How to update existing object in Core Data?