Basic data insertNewObjectForEntityForName and executeFetchRequest return nil

综合技术 Hello, buddy! (源链)

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.

您可能感兴趣的

一句代码实现银行卡手机号输入时格式化... 场景一、手机号/银行卡 格式化 核心代码 - (NSString *)PhoneFormatter:(NSString*)str { NSNumber * number = ]; NSNumberFormatter * formatter = ;//设置...
一行代码搞定:下拉放大图片, 导航栏颜色渐变, 添加子控制器, 左右滑动同时切换按钮... 目的 本文要跟大家分享的是一个带有多种效果的控制器,仅需一行代码即可创建。 这个控制器通过下拉可以放大顶部图片,上推可以使导航栏的颜色渐变,并且添加了三个子控制器,页面中间的三个按钮用来切换,当然,你也可以通过左右滑动来切换子控制器。 代码的下载地址: WHAddVC 接下来会分...
Everything You Need to Know About Ruby for iOS Dev... On iOS projects, we often find ourselves using command-line tools for testing and distributing our apps. More often than not, these tools were written...
100条 多年iOS开发经验总结(mark) 1、设置UILabel行间距 NSMutableAttributedString* attrString = initWithString:label.text]; NSMutableParagraphStyle *style = init]; ; ; label.attributedTe...
NSUserDefault is not registered I only want the alert to show up if the variable "genderDefault" has not been assigned yet. This code is suppose to save the choice of the user: f...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Basic data insertNewObjectForEntityForName and executeFetchRequest return nil



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

使用声明 | 英豪名录