核心数据不保存NSString [英] Core Data not saving NSString

查看:137
本文介绍了核心数据不保存NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的Core Data储存中发现问题。由于某种原因,NSString没有被正确保存,总是返回0,不管。

Currently seeing an issue in my Core Data saving. For some reason a NSString isn't being saved properly, always returning 0 regardless. Below is my current code.

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 valueForKey:@"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"];

                NSString *formattedTitle = [NSString decodeShit:notetitle];
                NSString *formattedSummary = [NSString decodeShit:notesummary];
                NSString *formattedContent = [NSString decodeShit:notecontent];


                NSLog(@"Note Title: %@",formattedTitle);
                NSLog(@"Note Summary: %@",formattedSummary);
                NSLog(@"Note ID: %@",noteid);
                NSLog(@"Note Content: %@",formattedContent);
                NSLog(@"Note Created: %@",notecreated);
                NSLog(@"Note Updated: %@",noteupdated);
                NSLog(@"Note Locked: %@",notelocked);

                newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
                [newNote autorelease];

                [newNote setValue:formattedContent forKey:@"content"];
                [newNote setValue:formattedSummary forKey:@"summary"];
                [newNote setValue:formattedTitle forKey:@"title"];
                [newNote setValue:noteid forKey:@"ID"];
                [newNote setValue:notecreated forKey:@"created"];
                [newNote setValue:noteupdated forKey:@"updated"];
                [newNote setValue:notelocked forKey:@"locked"];

                NSLog(@"Note Updated in MOC:%@",[newNote valueForKey:@"updated"]);

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


            }

            [noteFetch release];

        }

尝试保存更新字符串。由于某些原因,无论其中的字符串是什么,它都不会保存到Core Data属性。

The issue arises when trying to save the updated string. For some reason, regardless of what string is in there, it will not save to the Core Data attribute.

我努力找到任何相关的原因节省,但我要认为这是可能的。这是最后一个问题,需要在此应用程序可以打架之前排序。任何帮助都很棒!

I'm struggling to find any coherent reason for it to not be saving, but I'm about to deem it possible. This is the last issue that needs to be sorted before this application can hit the shelves. Any help would be great!

编辑

调试控制台中的以下输出。

There is no error, only the following output in the debug console.

 2011-04-24 10:53:22.815 Notacious[5660:707] Note Title: Hello there!
 2011-04-24 10:53:22.816 Notacious[5660:707] Note Summary: 
 2011-04-24 10:53:22.817 Notacious[5660:707] Note ID: 1240
 2011-04-24 10:53:22.817 Notacious[5660:707] Note Content: Hello there!

 This is a new note made from the iPhone client. Woot?
 2011-04-24 10:53:22.818 Notacious[5660:707] Note Created: Thu Apr 21 19:19:39 -0700 2011
 2011-04-24 10:53:22.819 Notacious[5660:707] Note Updated: Thu Apr 21 19:19:39 -0700 2011
 2011-04-24 10:53:22.820 Notacious[5660:707] Note Locked: 0
 2011-04-24 10:53:22.823 Notacious[5660:707] Note Updated in MOC:0


推荐答案

以下代码已生效。

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 *noteupdated = [singleNote valueForKey:@"note updated"];
                NSString *notecreated = [singleNote objectForKey:@"note created"];
                NSString *notetitle = [singleNote objectForKey:@"note title"];
                NSString *notesummary = [singleNote objectForKey:@"note summary"];
                NSString *noteid = [singleNote objectForKey:@"note id"];
                NSString *notecontent = [singleNote objectForKey:@"note content"];

                NSString *formattedTitle = [NSString decodeShit:notetitle];
                NSString *formattedSummary = [NSString decodeShit:notesummary];
                NSString *formattedContent = [NSString decodeShit:notecontent];

                newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];

                [newNote setValue:formattedContent forKey:@"content"];
                [newNote setValue:formattedSummary forKey:@"summary"];
                [newNote setValue:formattedTitle forKey:@"title"];
                [newNote setValue:noteid forKey:@"ID"];
                [newNote setValue:notecreated forKey:@"created"];
                [newNote setValue:noteupdated forKey:@"noteupdated"];
                [newNote setValue:notelocked forKey:@"locked"];

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


            }

            [noteFetch release];

        }

CoreData似乎不喜欢我访问属性已更新

It seems that CoreData didn't like me accessing an attribute with the name updated.

这篇关于核心数据不保存NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆