NSArray 中的核心数据错误

发布于 2025-01-03 07:22:46 字数 1949 浏览 2 评论 0原文

我有一个执行 NSFetchRequest 来获取托管对象数组(特别是 XMPPUserCoreDataStorageObjects)的方法。在performUserFetch返回数组之前,对象正确地发生故障,我可以打印所有的displayNames,但是一旦我将数组返回到printUserInfo,对象就会进入故障状态,除了Core Data不会带来问题之外,这不会是问题他们回来了!

- (NSArray*)performUserFetch 
{
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    [context setPersistentStoreCoordinator:[xmppRosterStorage persistentStoreCoordinator]];
    [context setUndoManager:nil];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" inManagedObjectContext:context];

    NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
    NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    NSError *err;

    NSArray *result = [context executeFetchRequest:fetchRequest error:&err];
    return result;

}

- (void)printUserInfo 
{
    NSArray *result = [self performUserFetch];
    for(XMPPUserCoreDataStorageObject *user in result)
    {
        NSString *dn = user.displayName;
        NSLog(@"Display name = %@", dn);
    }
        NSLog(@"%@",result);
}

我每 5 秒调用一次 printUserInfo,结果数组在 PerformUserFetch 中表现良好,但在 printUserInfo 中所有内容都已失效,并且数组中的所有元素都已从内存中清除。那很好,但是当我调用 user.displayName 时,错误没有解决,所以 dn 的值为 null,并且用户的描述是

"<XMPPUserCoreDataStorageObject: 0x10219fd60> (entity: XMPPUserCoreDataStorageObject; id: 0x1021a3390 <x-coredata://324B9E93-BAD1-42B4-B7DB-2A62CA69BA13/XMPPUserCoreDataStorageObject/p127> ; data: <fault>)"

Can Anybody help?!

(信息:10.7 SDK,printUserInfo每5秒调用一次,并且在ARC下运行)

I have a method that performs a NSFetchRequest to obtain an array of managed objects (specifically XMPPUserCoreDataStorageObjects). The objects fault correctly before the array is returned by the performUserFetch and I can print all the displayNames, but once I return the array to printUserInfo, the objects enter a fault state, which wouldn't be a problem except Core Data won't bring them back!

- (NSArray*)performUserFetch 
{
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    [context setPersistentStoreCoordinator:[xmppRosterStorage persistentStoreCoordinator]];
    [context setUndoManager:nil];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" inManagedObjectContext:context];

    NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
    NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    NSError *err;

    NSArray *result = [context executeFetchRequest:fetchRequest error:&err];
    return result;

}

- (void)printUserInfo 
{
    NSArray *result = [self performUserFetch];
    for(XMPPUserCoreDataStorageObject *user in result)
    {
        NSString *dn = user.displayName;
        NSLog(@"Display name = %@", dn);
    }
        NSLog(@"%@",result);
}

I call printUserInfo every 5 seconds, and the result array if fine in performUserFetch, but everything's gone to pot in printUserInfo, and all the elements in the array have been purged from memory. That would be fine, but when I call user.displayName the fault isn't resolved, so dn has a value of null, and the description of the user is

"<XMPPUserCoreDataStorageObject: 0x10219fd60> (entity: XMPPUserCoreDataStorageObject; id: 0x1021a3390 <x-coredata://324B9E93-BAD1-42B4-B7DB-2A62CA69BA13/XMPPUserCoreDataStorageObject/p127> ; data: <fault>)"

Can anyone help?!

(Info: 10.7 SDK, printUserInfo is called every 5 seconds, and is running under ARC)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情感失落者 2025-01-10 07:22:46

只是猜测。尝试以下方法是有希望的:

  1. 不要每次都重新创建托管对象上下文。将其保留为班级的财产。
  2. 也许您还应该分配/初始化数组以确保内存保持分配状态。您还可以使用财产。

另一种解决方案,顺便说一句,是使用 NSFetchedResultsController。它的设计在故障排除方面特别可靠和高效。

Just guessing. It would be promising to try the following

  1. Don't recreate a managed object context each time. Keep it as a property of your class.
  2. Maybe you should also alloc/init the array to be sure the memory stays allocated. You could also use a property.

An alternative solution, BTW, is to use an NSFetchedResultsController. It has been designed to be particularly reliable and efficient when it comes to faulting.

凉栀 2025-01-10 07:22:46

托管对象不会自动保留其上下文。当 -performUserFetch 返回并且 context 超出范围时,您的托管对象上下文将被垃圾收集。没有上下文,托管对象本身就没用。

只要您需要使用这些 XMPPUserCoreDataStorageObject 实例,就将上下文保留在某个地方(正如 Mundi 也建议的那样)。

Managed objects don't automatically retain their context. When -performUserFetch returns and context goes out of scope, your managed object context is garbage collected. Without the context, the managed objects themselves are useless.

Retain the context yourself somewhere (as Mundi also suggested) for as long as you need to use those XMPPUserCoreDataStorageObject instances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文