views:

71

answers:

3

I'm trying to set a value and then return it using Core Data. This is what I`m doing right now :

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

    NSManagedObject *newShot = [NSEntityDescription insertNewObjectForEntityForName:@"Shots" inManagedObjectContext:context];

    NSString *newName= @"test"; 
    [newShot setValue:newName forKey:@"shotNumber"];    

    NSError *error = nil;
    [context save:&error];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Shots" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
    [fetchRequest release];
    NSLog(@"test : %@",items);

Problem is when I want to check that it stored the data correctly by returning a NSLog of the array, this is what I'm getting :

 <NSManagedObject: 0x5bbbe10> (entity: Shots; id: 0x5b02b50 <x-coredata://F61F56A7-6A74-42AF-B565-6F9E091DA5F3/Shots/p14> ; data: <fault>)

Following the 2 answers, this is what I'm using :

int size = [items count];
NSLog(@"array count is %d ",size);

if (size > 0) {

    NSManagedObject *mo = [items objectAtIndex:0];  // assuming that array is not empty
    NSString *value = [[mo valueForKey:@"shotNumber"] stringValue];
    NSLog("Value : %@",value);

}

Just having this little issue :

warning: passing argument 1 of 'NSLog' from incompatible pointer type

How can I log [mo valueForKey:@"shotNumber"]?

+1  A: 

This is valid and correct. Because you didn't query any of its attributes or relationships, it has not been faulted in. You can either ask object at index 0 (if the result array isn't empty) for its -valueForKey:@"shotNumber" or force the faulting by setting the fetch request's -setReturnsObjectsAsFaults: to NO (it's YES by default).

Joshua Nozzi
right, that does make sense, I've edited the question
Julz
+1  A: 

A "fault" is simply an object that hasn't been loaded yet. Core Data uses faults to improve performance. Rather than load in an object's data immediately, Core Data represents that data with a fault and only retrieves the data when you try to access it. Your log message is just saying that Core Data hasn't loaded any data for your Shot object yet. Try accessing some properties of the Shot object and see what happens.

To address your second question, you've omitted the @ before the first double quote in your NSLog statement. It should read:

NSLog(@"Value : %@", value);
James Huddleston
duh ... Thanks !
Julz
A: 

Protip: Right-click NSLog and "Jump to Definition". This goes to the definition (i.e. source file) if available, and the declaration (i.e. header file) if it can't find the definition. The header will tell you what it's expecting.

NSLog expects an NSString* format string (i.e. NSLog(@"Foo: %@", bar)).

tc.
I guess, I should have been more precise, edited the question again
Julz