How to check if something is stored in CoreData

Posted by Terrel Gibson on Stack Overflow See other posts from Stack Overflow or by Terrel Gibson
Published on 2012-08-28T03:36:50Z Indexed on 2012/08/28 3:38 UTC
Read the original article Hit count: 229

Hi I want to be able to tore some information in core data and but i am unsure of how to check if the file was saved properly. I tried using NSLog but it returns null when its called. I have a dictionary which has a uniqueID and a title which I want to save. I pass this in along with the context of the database. I then sort the database to check if it has any duplicates or not, if not then I add the file.

+(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;

}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about xcode