【问题标题】:Retrieve Specific Object from Core Data从核心数据中检索特定对象
【发布时间】:2013-05-19 23:03:09
【问题描述】:

我是Core Data 的新手,想知道是否有可能根据它的属性获取对象,更具体地说,是我分配给它的唯一ID。我正在尝试这样做,因为我正在与 Web 服务器交互,该服务器提供将更新 Core Data 的数据。我想搜索每个 Web 服务器对象,检查时间戳,如果不同,则从核心数据中检索该对象并进行更新。我看过使用existingObjectWithId,但似乎我必须知道我正在搜索哪个对象,或者该对象的ID。我还考虑过对两个数组中的数据进行排序,然后同时检查每个数组,但认为这是不可行的。

这是我目前正在做的事情:

-(NSMutableArray*) updateRootBeers:(NSMutableArray*)webRootBeerList{
    NSManagedObjectContext* moc = [self managedObjectContext];
    NSFetchRequest* fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Rootbeer"];
    coreDataRootBeerList = [[moc executeFetchRequest:fetchRequest error:nil]mutableCopy];
    //check to see if core data has data, if not, call function to populate the list
    if (coreDataRootBeerList.count <=0) {
        [self addRootBeerToCoreData:webRootBeerList];
    }else{
        //otherwise I want to update the core data object if object data is different.
        for(RootBeer* currentRootBeer in webRootBeerList){
            RootBeer* mRootBeer = [moc existingObjectWithId:currentRootBeer error:nil];
        }
    }



}

我还考虑过使用嵌套的 for 循环来检查每个数组中的数据,但这似乎是糟糕的编码。 任何帮助或想法都会很棒。

【问题讨论】:

  • this question 可能会让你走上正轨
  • 我认为这可能行得通!谢谢!

标签: objective-c macos core-data


【解决方案1】:

您想创建一个NSFetchRequest。您可以设置实体并提供谓词。真的很简单干净。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    相关资源
    最近更新 更多