2014-03-28 73 views
0
NSArray *theArray = [[notification userInfo] objectForKey:@"myArray"]; 
for (NSDictionary * dict in theArray) { 

    CustomerOrder * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"CustomerOrder" 
                  inManagedObjectContext:self.managedObjectContext]; 

    [newEntry setValue:[dict objectForKey:@"customer_id"] forKey:@"customer_id"]; 
    [newEntry setValue:[dict objectForKey:@"ExecutedOrderCount"] forKey:@"executedOrderCount"]; 
    [newEntry setValue:[dict objectForKey:@"order_id"] forKey:@"order_id"]; 
    [newEntry setValue:[dict objectForKey:@"outletLatitude"] forKey:@"outletLatitude"]; 
    [newEntry setValue:[dict objectForKey:@"outletLongitude"] forKey:@"outletLongitude"]; 
    [newEntry setValue:[dict objectForKey:@"trigger_days"] forKey:@"trigger_days"]; 

    [newEntry setValue:[dict objectForKey:@"trigger_end_date"] forKey:@"trigger_end_date"]; 
    [newEntry setValue:[dict objectForKey:@"trigger_from_time"] forKey:@"trigger_from_time"]; 
    [newEntry setValue:[dict objectForKey:@"trigger_no_days"] forKey:@"trigger_no_days"]; 

    [newEntry setValue:[dict objectForKey:@"trigger_radius"] forKey:@"trigger_radius"]; 
    [newEntry setValue:[dict objectForKey:@"trigger_start_date"] forKey:@"trigger_start_date"]; 
    [newEntry setValue:[dict objectForKey:@"trigger_to_time"] forKey:@"trigger_to_time"]; 

    NSLog(@"Log %@", newEntry); 

    [SharedAppDelegate saveContext]; 
} 
+0

我建议你使用newEntry [@ “trigger_radius”] =字典[@ “trigger_radius” ]。它使你的代码更具可读性。另外,请说明如何保存上下文,并显示如何知道它未保存(即显示您的获取代码)。 – Joride

+0

我已经将[sharedAppDelegate savecontext]更改为 [newEntry.managedObjectContext save:&err] NSLog(@“error:%@”,err.localizedDescription);我已经完成了它,我不知道如何获取.I' m手动检查sqllite浏览器,显示没有找到表格。请提供一些帮助。 – user3420042

+0

新入口也许是零?你的日志语句的输出是什么?你还可以显示抓取代码吗?你检查BOOL的save:方法返回吗? – Joride

回答

-1

您在self.managedObjectContext创建实体,而是为节省话费[SharedAppDelegate saveContext];

你可以尝试后NSLog

NSError * err = nil; 
[newEntry.managedObjectContext save:&err]; 

NSLog(@"error: %@", err.localizedDescription); 

和循环调用保存在之后添加以下代码父上下文

__weak typeof (self) weakSelf; 
[self.managedObjectContext.parentContext performBlock:^(){ [weakSelf.managedObjectContext.parentContext save:NULL] }]; 
+0

错误越来越null.but在数据库中,如果我检查没有创建表,它显示。 – user3420042

+0

你如何检查? – sage444

+0

这是我的文件路径,我用sqllite浏览器打开它。 ///Users/vishnuemani/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/4BC11011-F80F-41C7-870A-99F3666E678F/Documents/CoreData.sqlite – user3420042

0

只是使用

NSEntityDescription *entityDesc=[NSEntityDescription entityForName:@"CustomerOrder" inManagedObjectContext:self.managedObjectContext]; 
NSFetchRequest *request=[[NSFetchRequest alloc]init]; 
[request setEntity:entityDesc]; 
NSManagedObject * newEntry =[[NSManagedObject alloc]initWithEntity:entityDesc insertIntoManagedObjectContext:self.managedObjectContext]; 
    .... 


NSError *error;     
[self.managedObjectContext save:&error]; 

希望这将有助于..

0

试试这个代码

NSArray *theArray = [[notification userInfo] objectForKey:@"myArray"]; 
for (NSDictionary * dict in theArray) { 

//TypeCast here 

CustomerOrder* newEntry = (CustomerOrder *)[NSEntityDescription insertNewObjectForEntityForName:@"CustomerOrder" 
                 inManagedObjectContext:self.managedObjectContext]; 

[newEntry setValue:[dict objectForKey:@"customer_id"] forKey:@"customer_id"]; 
[newEntry setValue:[dict objectForKey:@"ExecutedOrderCount"] forKey:@"executedOrderCount"]; 
[newEntry setValue:[dict objectForKey:@"order_id"] forKey:@"order_id"]; 
[newEntry setValue:[dict objectForKey:@"outletLatitude"] forKey:@"outletLatitude"]; 
[newEntry setValue:[dict objectForKey:@"outletLongitude"] forKey:@"outletLongitude"]; 
[newEntry setValue:[dict objectForKey:@"trigger_days"] forKey:@"trigger_days"]; 

[newEntry setValue:[dict objectForKey:@"trigger_end_date"] forKey:@"trigger_end_date"]; 
[newEntry setValue:[dict objectForKey:@"trigger_from_time"] forKey:@"trigger_from_time"]; 
[newEntry setValue:[dict objectForKey:@"trigger_no_days"] forKey:@"trigger_no_days"]; 

[newEntry setValue:[dict objectForKey:@"trigger_radius"] forKey:@"trigger_radius"]; 
[newEntry setValue:[dict objectForKey:@"trigger_start_date"] forKey:@"trigger_start_date"]; 
[newEntry setValue:[dict objectForKey:@"trigger_to_time"] forKey:@"trigger_to_time"]; 

NSLog(@"Log %@", newEntry); 

//Save all 
NSError *error = nil; 
if (![self.managedObjectContext save:&error]) { 
// Handle Error 
     NSLog(@"Save Error: %@, %@", error, [error userInfo]); 
    return NO; 
    } 

}