如何使用核心模型类创建临时对象

时间:2013-05-20 21:48:33

标签: iphone ios objective-c core-data

我是Core Data的新手,我遇到了创建然后删除对象的问题。我正在尝试从Web服务下载并解析JSON个Feed,然后将Feed数据与Core Data模型中的数据进行比较。我在Core Data模型中有两个模型:RootBeer& Comments。作为新人,我认为我可以像其他任何类一样使用RootBeer类,因为我可以初始化,然后暂时存储数据;然而,事实并非如此。然后我为每个根啤酒创建了一个新的上下文,然后尝试删除它们,但它似乎只删除了信息,而不是对象本身。当我获取Core Data时,它返回一个包含8个具有null个属性的对象的数组,而不是null

我的问题是A:我该怎么做才能从上下文中删除对象;或者,

B中。为了进行比较,您建议使用什么替代方法来完成临时数据?我会更好地创建另一个RootBeer类,用于存储JSON Feed中的数据吗?

任何建议或方向都会很棒!

以下是我创建删除对象的代码:

//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

0 个答案:

没有答案