gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:59:13 25 4
gpt4 key购买 nike

将数据(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com