将NSManagedObjects数组传递到新视图 [英] Pass array of NSManagedObjects to a new view

查看:79
本文介绍了将NSManagedObjects数组传递到新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我撕掉了我的头发,试图找出如何获取NSManagedObjects列表,然后将它们传递到另一个视图。

I'm tearing my hair out trying to figure out how to fetch a list of NSManagedObjects, and then passing them to another view.

这是我的fetch



Here's my fetch

+ (NSArray *)fetchListOfParentSectors;
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Sector"];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"parent_id = -1"]];

    NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
    [moc setPersistentStoreCoordinator: [[JobDataModel sharedDataModel] persistentStoreCoordinator]];

    NSError *error = nil;
    NSArray *results = [moc executeFetchRequest:fetchRequest error:&error];
    if (error) {
        NSLog(@"ERROR: %@ %@", [error localizedDescription], [error userInfo]);
        exit(1);
    }

    return results;
}

如果我在这里放一个循环记录sector.name, 。

If I put a loop in here to log sector.name here it works perfectly.

现在在我的视图控制器中我有这样:

Now in my view controller I have this:

NSArray *objectArray = [FetchSectors fetchListOfParentSectors];
for (int i = 0; i < objectArray.count; i++) {
    Sector *sector = (Sector *)[objectArray objectAtIndex: i];
    NSLog(@"name = %@", sector.name);
}
NSLog(@"objects = %@", objectArray);

名称返回的日志结果(null)。我知道数组不为空,因为返回的对象数量正确,日志显示了一堆列出的对象,如下所示:

The log results for name return (null). I know the array isn't empty as there are the correct amount of objects returned, and the log shows a bunch of objects listed like this:

< Sector:0xac91020>(entity:Sector; id:0xac8e900< x-coredata:// 19C94F15-0A5B-4A40-8E68-17FE6C4950F8 / Sector / p4> ;; data:< fault>) c $ c>

"<Sector: 0xac91020> (entity: Sector; id: 0xac8e900 <x-coredata://19C94F15-0A5B-4A40-8E68-17FE6C4950F8/Sector/p4> ; data: <fault>)"

有一些简单的东西,我可以忽略这里..

There has to be something simple that i'm overlooking here..

推荐答案

核心数据对象只能存在于受管对象上下文中。您在 fetchListOfParentSectors 中创建了一个MOC,但(假设您使用ARC编译)此MOC在函数结束时自动释放。因此,当您访问对象的属性 section.name 时,MOC不再存在。

Core data objects can only exist in a managed object context. You create a MOC locally in fetchListOfParentSectors, but (assuming that you compile with ARC) this MOC is automatically released at the end of the function. Therefore, when you access the object's attribute section.name, the MOC does not exist anymore.

预期一些运行时异常为这种情况,但实际上属性的访问器只返回 nil (我已经验证了一个小的测试程序。)

I would have expected some runtime exception for this situation, but actually the accessor for the attribute returns just nil (I have verified that with a small test program.)

因此,在 fetchListOfParentSectors 中使用本地MOC没有意义。您应该使用全局MOC,或在视图控制器中创建一个,并将其作为参数传递到您的fetch函数。

It makes therefore no sense to use a local MOC in fetchListOfParentSectors. You should use a global MOC, or create one in the view controller, and pass that as parameter to your fetch function.

这篇关于将NSManagedObjects数组传递到新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆