Issue with Core Data attribute returning 0 [duplicate]

痞子三分冷 提交于 2019-12-24 10:18:53

问题


Possible Duplicate:
Core Data not saving NSString.

Hey all!

Currently experiencing a weird issue with Core Data in my latest application. Basically what I'm accomplishing is that of parsing JSON and adding each object into Core Data under the respective attribute. This works fine as far as I can see from the NSLogs I have set in place. However, when it comes to displaying one of the values in a UITableView, it always returns 0 for some unknown reason.

Here is the function that I am using to store the objects into the Core Data entity.

-(void)syncNotes {

UIApplication *app = [UIApplication alloc];
app.networkActivityIndicatorVisible = YES;

NSDictionary *params = [NSDictionary dictionaryWithObject:authToken forKey:@"api_key"]; 

[[LRResty client] get:@"http://notacio.us/api/note" parameters:params withBlock:^(LRRestyResponse *response){

    if(response.status == 200) {
        NSLog(@"Successful Connection \n%@", [response asString]);

        // Create SBJSON object to parse JSON
        SBJSON *parser = [[SBJSON alloc] init];

        // parse the JSON string into an object - assuming [response asString] is a NSString of JSON data
        NSDictionary *object = [parser objectWithString:[response asString] error:nil];

        NSFetchRequest *noteFetch;
        NSManagedObject *newNote;

        appDelegate =(NotaciousAppDelegate *) [[UIApplication sharedApplication] delegate];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
        NSArray *fetchedNotes;

        NSArray *notes = [object valueForKey:@"result"];
        for (NSDictionary *singleNote in notes){

            noteFetch=[[NSFetchRequest alloc] init];
            [noteFetch setEntity:entity];
            NSPredicate *pred=[NSPredicate predicateWithFormat:@"ID==%@",[singleNote objectForKey:@"note id"]];
            [noteFetch setPredicate:pred];

            NSError *fetchError=nil;
            fetchedNotes=[appDelegate.managedObjectContext executeFetchRequest:noteFetch error:&fetchError];

            if (fetchError!=nil) {
                NSLog(@"syncNotes fetchError=%@,details=%@",fetchError,fetchError.userInfo);
            }
            if ([fetchedNotes count]==0) {


                NSString *notelocked = [singleNote objectForKey:@"note locked"];

                NSString *notecreated = [singleNote objectForKey:@"note created"];
                NSString *noteupdated = [singleNote objectForKey:@"note updated"];
                NSString *notetitle = [singleNote objectForKey:@"note title"];
                NSString *notesummary = [singleNote objectForKey:@"note summary"];
                NSString *noteid = [singleNote objectForKey:@"note id"];
                NSString *notecontent = [singleNote objectForKey:@"note content"];

                NSLog(@"Note Title: %@",notetitle);
                NSLog(@"Note Summary: %@",notesummary);
                NSLog(@"Note ID: %@",noteid);
                NSLog(@"Note Content: %@",notecontent);
                NSLog(@"Note Created: %@",notecreated);
                NSLog(@"Note Updated: %@",noteupdated);
                NSLog(@"Note Locked: %@",notelocked);


                newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
                [newNote setValue:notecontent forKey:@"content"];
                [newNote setValue:notesummary forKey:@"summary"];
                [newNote setValue:notetitle forKey:@"title"];
                [newNote setValue:noteid forKey:@"ID"];
                [newNote setValue:notecreated forKey:@"created"];
                [newNote setValue:noteupdated forKey:@"updated"];
                [newNote setValue:notelocked forKey:@"locked"];

                NSError *error = nil;
                if (![appDelegate.managedObjectContext save:&error]) {
                    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                    abort();
                }


            }

        }

        [noteFetch release];
        app.networkActivityIndicatorVisible = NO;

    }

    if (response.status == 404) {
        NSLog(@"FAIL\n%@", [response asString]);
        app.networkActivityIndicatorVisible = NO;
    }

}];

}

So the NSLog's from me outputting the strings that represent that of the objects parsed from the JSON and that are going to be stored are as follows.

2011-03-31 10:52:23.334 Notacious[755:707] Note Title: Business Draft
2011-03-31 10:52:23.335 Notacious[755:707] Note Summary: Make sure you get one to Pez by end of w
2011-03-31 10:52:23.336 Notacious[755:707] Note ID: 676
2011-03-31 10:52:23.336 Notacious[755:707] Note Content: Business Draft
Conclusion is about 150 words, the rest is for the body of the report.
2011-03-31 10:52:23.337 Notacious[755:707] Note Created: Thu Feb 24 17:02:13 -0800 2011
2011-03-31 10:52:23.339 Notacious[755:707] Note Updated: Thu Feb 24 17:10:43 -0800 2011
2011-03-31 10:52:23.339 Notacious[755:707] Note Locked: 0

As you can see, there are values set for everything. However, my issue gets a little weird here. When setting up the UITableViewCell and I try to grab the value for updated, it returns 0. I am calling the value like this:

NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

NSLog(@"%@",[[managedObject valueForKey:@"title"] description]);
NSLog(@"%@",[[managedObject valueForKey:@"updated"] description]);

NSString *updated = [[managedObject valueForKey:@"updated"] description];
NSString *parsedDate = [NSDate getNoteTime:updated];

NSLog(@"Before Format: %@",updated);
NSLog(@"After Format: %@",parsedDate);

Which in turn logs the following:

2011-03-31 10:56:22.544 Notacious[755:707] Business Draft
2011-03-31 10:56:22.545 Notacious[755:707] 0
2011-03-31 10:56:22.548 Notacious[755:707] time0
2011-03-31 10:56:22.549 Notacious[755:707] note:0, current:1301531182
2011-03-31 10:56:22.550 Notacious[755:707] Before Format: 0
2011-03-31 10:56:22.550 Notacious[755:707] After Format: ********

As you can see, the updated string returns 0. Can anyone shed any light as to why this would be happening?

EDIT

I tried to do it with the created valueForKey instead, and you'll never guess what happened. It worked perfectly fine. So the only thing I can think of is if when we're saving the value to the Core Data attribute, it isn't saving properly. Which is weird, because both the created and updated attributes are NSStrings. Actually, everything in the Core Data entity are NSStrings.


回答1:


The most likely explanation is that you have a mismatch between the object classes returned by the dictionary and the objects expected by the entity. E.g. noteUpdated is supposed to be a date but there is nothing in the code that enforces that. If you try to set a date using a string value it cannot parse, the date may report a value of zero. You should check the classes returned by the dictionaries to see if they are appropriate for the attributes they are assigned to.

Also, this use of description is unnecessary:

NSLog(@"%@",[[managedObject valueForKey:@"title"] description]);

... because NSLog sends the description message anyway. What you have here is the description of a string returned by description. That may or may not accurately reflect the value of the object. Just use:

NSLog(@"%@",[managedObject valueForKey:@"title"]);

When troubleshooting, I would recommend printing the managed objects directly like so:

NSLog(@"%@",manageObject);

... which will let you see everything in it's more raw state.



来源:https://stackoverflow.com/questions/5494062/issue-with-core-data-attribute-returning-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!