Reputation: 735
I'll try to expose my problem, because is a bit complex. I use Core Data and I have a problem with the data stored. When I use this code:
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"ItemMessage"];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
NSMutableArray *values = [[NSMutableArray alloc] init];
if (error == nil) {
for (int i = 0; i<results.count; i++) {
NSLog(@"results %@",[results objectAtIndex:i]);
ItemMessage *itemMessage = [results objectAtIndex:i];
[values addObject:itemMessage];
}
ecc. the problem is that the value printed by NSLog is correct (the "results" contains something) but the itemMessage contains always 0 key/value pairs (it seems empty).
To understand what is the problem I went back and saw that in insertNewObjectForEntityForName
I have also this problem, this is the code that I used when I save the messages data in Core Data:
for (id key in objectMessage) {
ItemMessage *itemmessage = [[ItemMessage alloc] init];
itemmessage.itemMessageId = [key objectForKey:@"itemMessageId"];
itemmessage.message = [key objectForKey:@"message"];
itemmessage.sender = [key objectForKey:@"sender"];
itemmessage.users = [key objectForKey:@"users"];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newMessage;
newMessage = [NSEntityDescription insertNewObjectForEntityForName:@"ItemMessage" inManagedObjectContext:context];
[newMessage setValue: itemmessage.itemMessageId forKey:@"itemMessageId"];
[newMessage setValue: itemmessage.message forKey:@"message"];
[newMessage setValue: itemmessage.sender forKey:@"sender"];
[newMessage setValue: itemmessage.users forKey:@"users"];
[context save:&error];
if (error != nil) {
NSLog(@"Coredata error");
}
The problem is that newMessage
after the insertNewObjectForEntityForName
and the setValue
contains also 0 key/value pairs.
Can you help me?
Upvotes: 7
Views: 6850
Reputation: 6011
You don't seem to insert the new managed objects correctly into the context.
It should be:
for (id key in objectMessage) {
NSManagedObjectContext *context = [appDelegate managedObjectContext];
ItemMessage *itemmessage = (ItemMessage*)[NSEntityDescription insertNewObjectForEntityForName:@"ItemMessage"
inManagedObjectContext:context];
itemmessage.itemMessageId = [key objectForKey:@"itemMessageId"];
itemmessage.message = [key objectForKey:@"message"];
itemmessage.sender = [key objectForKey:@"sender"];
itemmessage.users = [key objectForKey:@"users"];
}
//save your inserts
To create a class file for your managed objects you could:
Go to your model file (xcdatamodeld) ->
select an entity ->
from the menu select:
Editor-> Create NSManagedObjectSubclass -> select the entities your like class files for.
Now you will have managed objects you could access with ease (NSManagedObject subclass) and benefit from CoreData features.
Upvotes: 4
Reputation: 25459
When you insert to manage object contest you have to call save: method, also the saving method should looks something like that:
newMessage = [NSEntityDescription insertNewObjectForEntityForName:@"ItemMessage" inManagedObjectContext:context];
// 2
newMessage.property1 = self.firstNameTextfield.text;
newMessage.property2 = self.lastNameTextfield.text;
if (![context save:&error]) {
NSLog(@"Error: %@", [error localizedDescription]);
}
Upvotes: 2