【问题标题】:How to check if something is stored in CoreData如何检查某些内容是否存储在CoreData中
【发布时间】:2012-08-22 13:23:06
【问题描述】:

您好,我希望能够撕毁核心数据中的一些信息,但我不确定如何检查文件是否正确保存。我尝试使用 NSLog 但它在调用时返回 null 。我有一本字典,它有一个 uniqueID 和一个我想保存的标题。我将它与数据库的上下文一起传递。然后我对数据库进行排序以检查它是否有任何重复项,如果没有,则添加文件。

+(VacationPhoto*) photoWithFlickrInfo: (NSDictionary*) flickrInfo inManagedObjectContext: (NSManagedObjectContext*) context{

//returns the dictionary
NSLog(@"Photo To Store =%@", flickrInfo);


VacationPhoto * photo = nil;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"VacationPhoto"];
request.predicate = [NSPredicate predicateWithFormat:@"uniqueID = %@", [flickrInfo objectForKey:FLICKR_PHOTO_ID]];
NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:descriptor];

NSError *error = nil;
NSArray *matches =  [context executeFetchRequest:request error:&error];

if (!matches || [matches count] > 1) {
    // handle error
} else if ( [matches count] == 0){
    photo.title = [flickrInfo objectForKey:FLICKR_PHOTO_TITLE];
//Returns NULL when called 
    NSLog(@"title = %@", photo.title);
    photo.uniqueID = [flickrInfo objectForKey:FLICKR_PHOTO_ID];
//Returns NULL when called 
    NSLog(@"ID = %@", photo.uniqueID);

} else {
//If photo already exists this is called
    photo = [matches lastObject];
}

return photo;

}

【问题讨论】:

  • 我认为文件没有正确保存,因为我尝试使用相同的信息两次调用该方法,而不是说在第二次迭代期间找到了副本,而是进入 if 语句时没有这样的文件

标签: objective-c xcode core-data nspredicate nsfetchrequest


【解决方案1】:

查看核心数据更改的最简单方法是使用sqlite browser

转到模拟器文件夹,找到应用程序,转到文档并为您的文件夹打开

XCODE_SIMLATOR_XX

找到

应用程序

在那里找到你的应用,打开 Documents,然后找到 yourApp.sqlite, 用sqlite浏览器打开,检查你的所有改动很容易,不建议编辑数据结构,因为coredata不是sqlite,只是用sqlite保存数据,但你必须习惯核心数据的对象方法;)

祝你好运

【讨论】:

  • 我发现了问题,但你的解决方案看起来很有趣,我会研究一下,+rep
【解决方案2】:

原来我在将信息添加到数据库时没有调用方法 insertNewObjectForEntityForName,这就是为什么没有保存任何内容的原因。在我添加它之后,NSLogs 提供了正确的信息

photo = [NSEntityDescription insertNewObjectForEntityForName:@"VacationPhoto" inManagedObjectContext:context];
    photo.title = [flickrInfo objectForKey:FLICKR_PHOTO_TITLE];
    NSLog(@"title = %@", photo.title);
    photo.uniqueID = [flickrInfo objectForKey:FLICKR_PHOTO_ID];
    NSLog(@"ID = %@", photo.uniqueID);

【讨论】:

    猜你喜欢
    • 2011-10-11
    • 2021-08-11
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2012-03-22
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多