我是新手,Core Data
在创建然后删除对象时遇到问题。我正在尝试JSON
从 Web 服务下载和解析提要,然后将提要数据与Core Data
模型中的数据进行比较。我在核心数据模型中有 2 个模型:RootBeer
& Comments
。作为新手,我想我可以RootBeer
像使用其他类一样使用该类,因为我可以初始化,然后临时存储数据;然而,事实并非如此。因此,我为每个根啤酒创建了一个新的上下文,然后尝试删除它们,但似乎它只删除了信息,而不是对象本身。当我获取核心数据时,它返回一个包含 8 个具有null
属性的对象的数组,而不是一个null
.
我的问题要么是 A:我该怎么做才能从上下文中删除对象;或者,
B. 为了比较,你建议用什么替代方法来完成临时数据?我是否会更好地创建另一个RootBeer
用于容纳来自JSON
提要的数据的类?
任何建议或方向都会很棒!
这是我创建删除对象的代码:
//rootBeerList is JSON feed
for(NSDictionary* rootBeer in rootBeerList){
//create moc to store temp root beer object
NSManagedObjectContext* moc = [self managedObjectContext];
RootBeer* mRoot = [NSEntityDescription insertNewObjectForEntityForName:@"RootBeer" inManagedObjectContext:moc];
mRoot.rootBeerName = [rootBeer objectForKey:@"Name"];
mRoot.rootBeerBrewer = [rootBeer objectForKey:@"Brewer"];
mRoot.rootBeerId = [rootBeer objectForKey:@"Id"];
mRoot.rootBeerDescription = [rootBeer objectForKey:@"Description"];
mRoot.rootBeerDetails = [rootBeer objectForKey:@"ProductDetails"];
//mRoot.hasCaneSugar = [ rootBeer objectForKey:@"Sugar"];
//mRoot.hasCaffeine = [ rootBeer objectForKey:@"Caffeine"];
mRoot.rootBeerLocation = [ rootBeer objectForKey:@"Region"];
mRoot.rootBeerRating = [ rootBeer objectForKey:@"avgRating"];
mRoot.updatedAt = [rootBeer objectForKey:@"updatedAt"];
NSLog(@"Name: %@", mRoot.rootBeerName);
NSLog(@"Name: %@", mRoot.rootBeerBrewer);
NSLog(@"Name: %@", mRoot.rootBeerId);
NSLog(@"Name: %@", mRoot.rootBeerDescription);
NSLog(@"Name: %@", mRoot.rootBeerDetails);
NSLog(@"Caffeine: %@", mRoot.hasCaffeine);
NSLog(@"Sugar: %@", mRoot.hasCaneSugar);
NSLog(@"avgRating: %@", mRoot.rootBeerRating);
NSLog(@"updatedAt: %@", mRoot.updatedAt);
//add object to array
[tempArray addObject:mRoot];
//delete object
[moc deleteObject:mRoot];
//save changes
if(![moc save:&error]){
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
}
}
我注意到的另一个问题是,即使我将对象保存到临时数组后,它仍然被删除,我不知道为什么
更新:
2013-05-20 14:34:31.622 RootBeerRecorder[54904:c07] web Root LIst (
"<RootBeer: 0x835d5b0> (entity: RootBeer; id: 0x835d610 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA0302> ; data: <fault>)",
"<RootBeer: 0x81512d0> (entity: RootBeer; id: 0x81544f0 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA0303> ; data: <fault>)",
"<RootBeer: 0x81543d0> (entity: RootBeer; id: 0x8153aa0 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA0304> ; data: <fault>)",
"<RootBeer: 0x81545b0> (entity: RootBeer; id: 0x81545f0 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA0305> ; data: <fault>)",
"<RootBeer: 0x8154680> (entity: RootBeer; id: 0x8153ca0 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA0306> ; data: <fault>)",
"<RootBeer: 0x8154960> (entity: RootBeer; id: 0x813a470 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA0307> ; data: <fault>)",
"<RootBeer: 0x8154810> (entity: RootBeer; id: 0x8153db0 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA0308> ; data: <fault>)",
"<RootBeer: 0x7460280> (entity: RootBeer; id: 0x7460190 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA0309> ; data: <fault>)"
)
2013-05-20 14:34:31.623 RootBeerRecorder[54904:c07] core Root LIst (
)
2013-05-20 14:34:31.624 RootBeerRecorder[54904:c07] Created Root Beer: <RootBeer: 0x81549f0> (entity: RootBeer; id: 0x8154940 <x-coredata:///RootBeer/tD0DD60D5-008D-49C0-AB9F-F1D035DBA03010> ; data: {
comments = nil;
hasCaffeine = nil;
hasCaneSugar = nil;
isFavorite = nil;
myRootBeerRating = 0;
rootBeerBrewer = nil;
rootBeerDescription = nil;
rootBeerDetails = nil;
rootBeerId = 0;
rootBeerLocation = nil;
rootBeerName = nil;
rootBeerRating = nil;
updatedAt = nil;
})
... this continues