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];
}
回答
您在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] }];
错误越来越null.but在数据库中,如果我检查没有创建表,它显示。 – user3420042
你如何检查? – sage444
这是我的文件路径,我用sqllite浏览器打开它。 ///Users/vishnuemani/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/4BC11011-F80F-41C7-870A-99F3666E678F/Documents/CoreData.sqlite – user3420042
只是使用
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];
希望这将有助于..
试试这个代码
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;
}
}
- 1. 核心数据数据库不保存
- 2. Mac OSX - 核心数据未被存储
- 3. 存储NSAttributedString核心数据
- 4. 核心数据不存在
- 5. 核心数据对象不会存储
- 6. 核心数据存储不必要的数据
- 7. 与核心数据的数据库
- 8. 核心数据:删除存储在核心数据中的视图
- 9. 将核心数据存储在NSOperation中
- 10. 在核心数据中存储ABRecordRef
- 11. 在核心数据中存储MKRoute
- 12. 在核心数据中存储NSInteger
- 13. 在核心数据中存储UUID
- 14. 在核心数据中存储NSDate(swift)
- 15. 在iPhone上的核心数据中存储二进制数据
- 16. 如何查看存储在核心数据中的数据?
- 17. 核心数据一对多关系不存储其数据
- 18. 不在数据库中存储数据
- 19. 存储核心数据的日期
- 20. 核心数据/数据库IOS
- 21. 核心数据保存到数据库不工作
- 22. 如何在核心数据中存储CLPlacemark地址数据
- 23. 将XML数据存储在核心数据中
- 24. Xcode-将数据存储在核心数据或SQLite中?
- 25. 我的数据库数据在哪里(核心数据,iOS)
- 26. 未保存核心数据数据
- 27. 在核心数据数据模型中有效地存储相同的数据
- 28. 核心数据支持的非核心数据数据UITableView
- 29. iOS核心数据:关于核心数据和数据库的困惑
- 30. 来自远程数据源的核心数据存储
我建议你使用newEntry [@ “trigger_radius”] =字典[@ “trigger_radius” ]。它使你的代码更具可读性。另外,请说明如何保存上下文,并显示如何知道它未保存(即显示您的获取代码)。 – Joride
我已经将[sharedAppDelegate savecontext]更改为 [newEntry.managedObjectContext save:&err] NSLog(@“error:%@”,err.localizedDescription);我已经完成了它,我不知道如何获取.I' m手动检查sqllite浏览器,显示没有找到表格。请提供一些帮助。 – user3420042
新入口也许是零?你的日志语句的输出是什么?你还可以显示抓取代码吗?你检查BOOL的save:方法返回吗? – Joride