Basic data insertNewObjectForEntityForName and executeFetchRequest return nil

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?

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.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Basic data insertNewObjectForEntityForName and executeFetchRequest return nil

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录