【发布时间】:2011-02-26 11:38:29
【问题描述】:
NSManagedObjectModel 由以下部分组成:
#import <CoreData/CoreData.h>
@interface Event : NSManagedObject
{
}
@property (nonatomic, retain) NSString * summary;
@property (nonatomic, retain) NSString * content;
@property (nonatomic, retain) NSDate * updated;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSDate * created;
@property (nonatomic, retain) NSString * ID;
@end
我正在尝试将解析后的 JSON 插入到上述核心数据属性中。
在下面,newNote 是我给 NSManagedObject 起的名字。基本上我想要完成的是将 JSON 插入核心数据,以便我可以在 UITableView 中显示它。以下函数用于解析 JSON 并将其插入 Core Data。
-(void)pullNotes {
UIApplication *app = [UIApplication alloc];
app.networkActivityIndicatorVisible = YES;
NSDictionary *params = [NSDictionary dictionaryWithObject:api_key forKey:@"api_key"];
[[LRResty client] get:@"http://notacio.us/api/note" parameters:params withBlock:^(LRRestyResponse *response){
if(response.status == 200) {
NSLog(@"Pulling the users notes \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];
NSArray *notes = [object valueForKey:@"result"];
for (NSDictionary *singleNote in notes){
// newNote.created = [singleNote objectForKey:@"note created"]; Need to work on parsing these properly...
// newNote.updated = [singleNote objectForKey:@"note updated"]; Need to work on parsing these properly...
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);
[newNote setValue:notecontent forKey:@"content"];
NSError *error = nil;
if (![newNote.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSLog(@"Added Core Data Value: %@",[newNote valueForKey:@"content"]);
// NSDate *createdDate =
// NSDate *updatedDate =
[tableView reloadData];
app.networkActivityIndicatorVisible = NO;
}
}
if (response.status == 404) {
NSLog(@"FAIL\n%@", [response asString]);
app.networkActivityIndicatorVisible = NO;
}
}];
}
字符串的 NSLogs 正在返回正确的东西,所以我知道它实际上应该将它们插入到核心数据中......
所以,我的问题。
当该函数被调用时,我得到以下错误。
2011-02-26 21:37:30.797 Notacious[22570:207] Unresolved error (null), (null)
我不明白发生了什么,希望有人能够阐明我的问题。
编辑
玩了几个小时后,我的功能有点儿用了:
-(void)pullNotes {
UIApplication *app = [UIApplication alloc];
app.networkActivityIndicatorVisible = YES;
NSDictionary *params = [NSDictionary dictionaryWithObject:api_key forKey:@"api_key"];
[[LRResty client] get:@"http://notacio.us/api/note" parameters:params withBlock:^(LRRestyResponse *response){
if(response.status == 200) {
NSLog(@"Pulling the users notes \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];
appDelegate =(NotaciousAppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObject *newNote;
NSArray *notes = [object valueForKey:@"result"];
for (NSDictionary *singleNote in notes){
// newNote.created = [singleNote objectForKey:@"note created"]; Need to work on parsing these properly...
// newNote.updated = [singleNote objectForKey:@"note updated"]; Need to work on parsing these properly...
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);
newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:appDelegate.managedObjectContext];
[newNote setValue:notecontent forKey:@"content"];
[newNote setValue:notesummary forKey:@"summary"];
[newNote setValue:notetitle forKey:@"title"];
[newNote setValue:noteid forKey:@"ID"];
NSLog(@"Added Core Data Value: %@",[newNote valueForKey:@"content"]);
// NSDate *createdDate =
// NSDate *updatedDate =
app.networkActivityIndicatorVisible = NO;
}
NSError *error = nil;
if (![appDelegate.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
if (response.status == 404) {
NSLog(@"FAIL\n%@", [response asString]);
app.networkActivityIndicatorVisible = NO;
}
}];
}
但是,无论实体之前是否存在,它都会添加实体。我想要它做的是发现如果一个笔记已经存在,更新它。如果没有,请创建一个新的。
编辑
目前这个函数看起来是这样的:
-(void)pullNotes {
UIApplication *app = [UIApplication alloc];
app.networkActivityIndicatorVisible = YES;
NSDictionary *params = [NSDictionary dictionaryWithObject:api_key forKey:@"api_key"];
[[LRResty client] get:@"http://notacio.us/api/note" parameters:params withBlock:^(LRRestyResponse *response){
if(response.status == 200) {
NSLog(@"Pulling the users notes \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];
appDelegate =(NotaciousAppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObject *newNote;
NSEntityDescription *noteEntity=[NSEntityDescription entityForName:@"Event" inManagedObjectContext:appDelegate.managedObjectContext];
NSFetchRequest *noteFetch;
NSArray *fetchedNotes;
NSArray *notes = [object valueForKey:@"result"];
for (NSDictionary *singleNote in notes){
noteFetch=[[NSFetchRequest alloc] init];
[noteFetch setEntity:noteEntity];
NSPredicate *pred=[NSPredicate predicateWithFormat:@"noteID==%@",[singleNote objectForKey:@"note id"]];
[noteFetch setPredicate:pred];
NSError *fetchError=nil;
fetchedNotes=[appDelegate.managedObjectContext performFetch:&fetchError];
if (fetchError!=nil) {
NSLog(@"pullNotes fetchError=%@,details=%@",fetchError,fetchError.userInfo);
}
if ([fetchedNotes count]==0) {
// newNote.created = [singleNote objectForKey:@"note created"]; Need to work on parsing these properly...
// newNote.updated = [singleNote objectForKey:@"note updated"]; Need to work on parsing these properly...
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);
newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:appDelegate.managedObjectContext];
[newNote setValue:notecontent forKey:@"content"];
[newNote setValue:notesummary forKey:@"summary"];
[newNote setValue:notetitle forKey:@"title"];
[newNote setValue:noteid forKey:@"ID"];
// NSDate *createdDate =
// NSDate *updatedDate =
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;
}
}];
}
然而,我在调用appDelegate.managedObjectContext 时似乎遇到了一个错误,它只是错误地说出以下内容:
2011-03-03 12:11:01.616 Notacious[182:307] -[NSManagedObjectContext performFetch:]: unrecognized selector sent to instance 0x1474e0
2011-03-03 12:11:01.781 Notacious[182:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObjectContext performFetch:]: unrecognized selector sent to instance 0x1474e0'
我已经尝试将其设置为appDelegate.managedObjectContext_,但它似乎返回了一个错误,指出在 appDelegate 中找不到它并且它不是一个 getter 对象。不过这里明确分配了:http://cl.ly/4xdX
编辑
我想我最好编辑这个以添加到我当前的工作中,并在原始标题下添加另一个问题。以下代码是我构建的并且运行良好(除了编辑后的字符串没有被保存,进而导致我的以下问题here)。
-(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];
[parser release];
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"];
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 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"];
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;
}
}];
}
现在,确定这可行,但是当内容字符串不同时,我想更新正确的注释。这里有没有人愿意详细说明如何做到这一点?
我知道我一直在这里问问题,但希望一切都完美,似乎我对此类简单任务的了解有些不存在。任何意见将不胜感激!
编辑
啊,另一个请求。如果当前存储的笔记没有 noteID,那么从核心数据中删除它?这可能吗?
【问题讨论】:
-
如何分配
newNote? -
我是这样做的 -> cl.ly/4rz2
-
附注:您忘记将
Event类重命名为其他名称,例如Note;)
标签: iphone objective-c cocoa-touch core-data nsmanagedobject