ios - 托管对象值随机为空(包括示例项目)

标签 ios core-data

将数据(10 条记录)保存到实体后,我正在处理获取请求以再次获取所有数据:

//Saving data
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    //Save to coredata


        song = [NSEntityDescription insertNewObjectForEntityForName:@"Song"
                                             inManagedObjectContext:context];
        [song setValue:title forKey:@"title"];

        [song setValue:songLink forKey:@"songWebLink"];
        NSLog(@"Song link : %@",songLink);//Never get NULL


        [song setValue:albumLink forKey:@"albumImageLink"];

        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }else{

            NSLog(@"Record saved correctly");
        }
}

上面的保存工作正常,我在保存到上下文之前非常仔细地调试了所有数据,以确保没有任何属性为 NULL

问题总是出在 songWebLink 属性上,有时当我尝试将它取回时它会变成 null:

- (void)parserDidEndDocument:(NSXMLParser *)parser{

    NSEntityDescription *songEntity=[NSEntityDescription entityForName:@"Song" inManagedObjectContext:context];
    NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
    [fetch setEntity:songEntity];
    NSError *fetchError;
    NSArray *fetchedSongs=[context executeFetchRequest:fetch error:&fetchError];

    NSMutableArray *songs = [[NSMutableArray alloc] init];

    for (NSManagedObject *songObject in fetchedSongs) {
    //here is the issue: this for loop will go through 10 iterations, songWebLink is randomly NULL, sometimes it's the fourth iteration, sometimes the 8th, sometimes the 5th.
        NSLog(@"song web link: %@",[songObject valueForKey:@"songWebLink"]);//albumImageLink and title attributes are fine
    }
}

问题是,当我 NSLog songWebLink 时,它得到 NULL,一次是第 4 次迭代,然后是第 6 次,然后是第 2 次等等。它随机分配 NULL 给获取时的属性。保存数据时,我确保 songWebLink 没有 NULL 值。所以我打赌其他原因会导致这个问题。

有什么想法吗?

编辑

这是 MOC 的初始化方式:

AppDelegate.m:

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];//I tried initWithConcurrencyType:NSMainQueueConcurrencyType
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

获取 MOC 对象以在不同的类中使用它:

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
context = [appDelegate managedObjectContext];

示例项目: 如果你觉得有必要看一下app工程,我做了一个简化版,我在上面复现了bug,你可以下载here .

最佳答案

试一试:

将其包含在您的获取请求中

[fetch setIncludesPropertyValues:YES];

像这样遍历返回的对象:

for (Song *songObject in fetchedSongs) {
    //here is the issue: this for loop will go through 10 iterations, songWebLink is randomly NULL, sometimes it's the fourth iteration, sometimes the 8th, sometimes the 5th.
    NSLog(@"song web link: %@",songObject.songWebLink);//albumImageLink and title attributes are fine
}

我不是 100% 确定这会解决它,但如果没有解决问题,它会帮助找到问题。

关于ios - 托管对象值随机为空(包括示例项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17803921/

相关文章:

ios - 模态转换 - 其中一个 View 似乎正在被释放

iOS - 将文件从另一个应用程序导入我的应用程序

objective-c - NSManagedObject 基类的选择器无法识别

ios - 在 Core Data 中设置实体之间关系的原因

ios - 检索托管对象并更改属性值

iphone - 通过 tableview 添加 View (UITableViewController)

ios - 如何强制 NSLocalizedString 使用特定语言

ios - UICollectionview 具有动态数据的自定义布局

ios - 如何在 CoreData 中为包含 NSArray 的二进制数据创建 NSPredicate?

ios - 如何使用 swift 将 MKPolyline 属性存储为可转换的 IOS 核心数据?