我的插入数据的代码如下(它从服务器接收json数据,解析它并插入它)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[myButton setTitle:@"Get o/p from file" forState:UIControlStateNormal];
JsonStorage *event = (JsonStorage *)[NSEntityDescription insertNewObjectForEntityForName:@"JsonStorage" inManagedObjectContext:managedObjectContext];
NSDictionary *json=[NSJSONSerialization JSONObjectWithData:mutabledata options:NSJSONReadingMutableContainers error:nil];
NSDictionary *message=[json objectForKey:@"message"];
NSMutableString *stringtemp=[[NSMutableString alloc] init];
//[temp appendString:@"Message: \n"];
for(id key in message)
{
[stringtemp appendString:[[NSString alloc] initWithFormat:@"%@ is %@ \n",key,message[key]]];
}
[event setMessage1:[[NSString alloc] initWithString:stringtemp]];
[event setApplicationid:[[NSString alloc] initWithFormat:@"application id is %@",json[@"applicationid"]]];
[event setRankings:[[NSString alloc] initWithFormat:@"Rankings for %@",json[@"rankings"]]];
NSArray *array=[json objectForKey:@"ranks"];
NSMutableString *temp=[[NSMutableString alloc] init];
for(int i=0;i<[array count];i++)
{
NSDictionary *rank=array[i];
[temp appendString:[[NSString alloc] initWithFormat: @"username is %@\n",rank[@"user_name"]] ];
[temp appendString:[[NSString alloc] initWithFormat: @"email id is %@\n",rank[@"user_email"]] ];
int inc=i+1;
[temp appendString:[[NSString alloc] initWithFormat: @"%@",rank[[[NSString alloc] initWithFormat:@"%d",inc ]]] ];
[temp appendString:[[NSString alloc] initWithFormat: @"\n"]];
}
[event setRankdetails:temp];
NSLog(@"asdasfafew %@",[event rankdetails]);
NSError *error;
if (![managedObjectContext save:&error]) {
// This is a serious error saying the record could not be saved.
// Advise the user to restart the application
NSLog(@"error");
}
[eventArray insertObject:event atIndex:0];
secondlabel.text=[NSString stringWithFormat:@" Date received, It is stored in the core data\n " ];
}
正确插入值,然后我的刷新视图函数(从核心数据中获取数据)如下所示
- (void) RefreshView
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"JsonStorage" inManagedObjectContext:managedObjectContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Fetch the records and handle an error
NSError *error;
NSMutableArray *mutableFetchResults;
[mutableFetchResults removeAllObjects];
mutableFetchResults= [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchResults) {
NSLog(@"too big error");
// Handle the error.
// This is a serious error and should advise the user to restart the application
}
// Save our fetched data to an array
[self setEventArray: mutableFetchResults];
NSMutableString *temp2=[[NSMutableString alloc] init];
//temp2=nil;
for (NSManagedObject *info in eventArray) {
// NSLog(@"reached refreshview");
NSString *temp=[[NSString alloc] initWithFormat:@"%@",[info valueForKey:@"message1"]];
//NSLog(@"Value is %@",temp);
[temp2 appendString:temp];
// [temp2 appendString:@"\n"];
//NSLog(@"reached refreshview1");
NSString *temp3=[[NSString alloc] initWithFormat:@"%@",[info valueForKey:@"applicationid"]];
[temp2 appendString:temp3];
// [temp2 appendString:@"\n"];
//NSLog(@"reached refreshview2");
NSString *temp4=[[NSString alloc] initWithFormat:@"%@",[info valueForKey:@"rankings"]];
[temp2 appendString:temp4];
// [temp2 appendString:@"\n"];
//NSLog(@"reached refreshview3");
NSString *temp5=[[NSString alloc] initWithFormat:@"%@",[info valueForKey:@"rankdetails"]];
[temp2 appendString:temp5];
}
//NSLog(@"final temp is %@",temp2);
NSString *finalstring=[temp2 stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
//NSLog(@"final string is is %@",finalstring);
secondlabel.text=finalstring;
}
temp2保持打印(null)(null).... 提前致谢