-1

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;

}
Terrel Gibson
  • 481
  • 1
  • 6
  • 21
  • I don't think the files are saving properly because I tried calling the method twice using the same information and instead of saying a copy has been found during the second iteration it instead goes into the if statement when there is no such file – Terrel Gibson Aug 28 '12 at 03:57

2 Answers2

0

the easiest way to see your core data changes, is to use a sqlite browser

go to the simulator folder, find the app, go to documents and open the folder for your

XCODE_SIMLATOR_XX

find

Applications

and find there your app, open Documents, and find yourApp.sqlite, open it with the sqlite browser, and check all your changes easy, not recommended to edit the data structure, as coredata is not sqlite, just uses sqlite to save the data, but you will have to get used to the object approach for core data ;)

good luck

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
0

Turns out I didnt call the method insertNewObjectForEntityForName when I was adding the information into the data base, this is why nothing was being saved. After I added it the NSLogs were providing the proper information

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);
Terrel Gibson
  • 481
  • 1
  • 6
  • 21