Ios fetchedObjects(NSArray)计数在';它到处都是东西

Ios fetchedObjects(NSArray)计数在';它到处都是东西,ios,objective-c,core-data,Ios,Objective C,Core Data,我的核心数据中有两个托管对象:CoreDataTrap&coredataalltrap。 到目前为止,CoreDataTrap的更新、添加、删除、保存等工作进展顺利。 但是,当我尝试使用第二个托管对象时,我没有错误描述,只有一个简单的获取对象的nil数组 混用CoreDataAllTraps的示例: NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NS

我的核心数据中有两个托管对象:
CoreDataTrap
&
coredataalltrap
。 到目前为止,CoreDataTrap的更新、添加、删除、保存等工作进展顺利。 但是,当我尝试使用第二个托管对象时,我没有错误描述,只有一个简单的获取对象的nil数组

混用
CoreDataAllTraps
的示例:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:kCORE_DATA_ALL_TRAPS_ENTITY inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil || fetchedObjects.count == 0) {
        NSLog(@"localizedDescription: %@, userInfo: %@", error.localizedDescription, error.userInfo);
        success = NO;
    }


    NSLog(@"%s success/fail: %@", __PRETTY_FUNCTION__, success?@"yes":@"no");
    NSLog(@"fetchedObjects.count: %d", fetchedObjects.count);
记录此日志:FetchedObject.count:0

我记录了一个没有任何详细信息的错误,
fetchedObjects.count=0
。 我试图创建一个新的NSManagedObjectContext,并引用AppDelegate中的一个,但都不起作用

编辑:

  • 第一次向
    CoreDataAllTraps
    添加对象(在一个类中)进行得很顺利,因为我记录了对象并看到了正确的细节
  • 我的案例是
    fetchedObjects.count=0
    (在其他类中)
  • 编辑-2:

    从不同类别中保存(非常有用):

    然后阅读不同的课程,确保一切正常(效果很好):

    记录以下内容:

    fetchedObjects.count: 222
    
    更新-1:


    我尝试了其他方法:当我将对象保存到核心数据时,我从原始保存函数的末尾调用该函数。这种方式看起来很糟糕,但它确实有效。我仍然在寻找一种漂亮的方法来搞乱MOC,而不必从不同的函数末尾调用函数。

    您是否创建了任何CoreDataAllTraps?在
    FetchedObject==nil
    (这意味着一个错误,应该设置
    error
    )和
    FetchedObject.count==0
    (这意味着没有错误,但找到了结果)。-这里发生了什么情况?我更新了我的问题。(当然,我在上一条评论中的意思是“但没有找到结果”)。添加对象后,您确实保存了上下文,对吗?。而“其他类”,它与添加对象的类具有相同的对象上下文?
    - (void)saveArray
    {
        // [Netroads sharedInstance].arrayOfAllTraps = [dbat getAllTraps];
        [Netroads sharedInstance].arrayOfAllTraps = self.allTraps;
        NSLog(@"[Netroads sharedInstance].arrayOfAllTraps.count: %i", [Netroads sharedInstance].arrayOfAllTraps.count);
    
        self.managedObjectContext = appDelegate.managedObjectContext;
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:kCORE_DATA_ALL_TRAPS_ENTITY inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];
        NSError *error = nil;
        NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
        NSLog(@"fetchedObjects.count: %d", fetchedObjects.count);
    
    fetchedObjects.count: 222