完全披露,我涉及目标C,所以这可能是非常明显的,但我找不到类似的问题或更准确地回答类似的问题。
我正在编写一个非常简单的应用程序,我正在学习核心数据,我有几个问题
Q.1已经回答了,所以这里是Q.2
Q2。从Core Data获取时,我检索空对象?
数据控制器与上述问题相同,我打电话给
- (NSMutableArray*) returnTimers
{
[self initilizeDataLayer];
NSMutableArray *listOfTimers = [[NSMutableArray alloc]init];
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Timer"];
NSError *error = nil;
NSArray *listOfTimersRaw = [myDataController.managedObjectContext executeFetchRequest:request error:&error];
if (error != nil) {
//Deal with failure
}
else {
//Deal with success
listOfTimers = [NSMutableArray arrayWithArray:listOfTimersRaw];
}
return listOfTimers;
}
这正确检索了2个对象,但它们是空的?
再次这是实验,所以可能是方法,但我必须以某种方式学习.....任何帮助将不胜感激。
Q1。将对象保存到核心数据会在sqlite db中创建一个空行 - 为什么?
我在Apple Docs中设置了一个DataController对象
- (void)initializeCoreData
{
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"WorkRestWork" withExtension:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSAssert(mom != nil, @"Error initializing Managed Object Model");
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[moc setPersistentStoreCoordinator:psc];
[self setManagedObjectContext:moc];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [documentsURL URLByAppendingPathComponent:@"WorkRestWork.sqlite"];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSError *error = nil;
NSPersistentStoreCoordinator *psc = [[self managedObjectContext] persistentStoreCoordinator];
NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:_options error:&error];
NSAssert(store != nil, @"Error initializing PSC: %@\n%@", [error localizedDescription], [error userInfo]);
});
}
然后我称这个方法
- (BOOL) addTimerWithName: (NSString*)timerName
{
[self initilizeDataLayer];
Timer *newTimer = [NSEntityDescription insertNewObjectForEntityForName:@"Timer" inManagedObjectContext:myDataController.managedObjectContext];
newTimer = [NSEntityDescription insertNewObjectForEntityForName:@"Timer" inManagedObjectContext:myDataController.managedObjectContext];
newTimer.name = timerName;
[myDataController.managedObjectContext insertObject:newTimer];
NSError *error = nil;
if ([myDataController.managedObjectContext save:&error] == NO) {
NSAssert(NO, @"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
return NO;
}
return YES;
}
最后
[cdh addTimerWithName:@"A TEST"];
当我检查使用Base检查.sqlite文件时,有两行,一行是“A TEST”作为名称而另一行是空的,这让我感到困惑
答案 0 :(得分:1)
广告Q1。 你在这里插入两个定时器。但是,您只为第二个指定名称。第一个没有名字,因为你从未设置它。这是你的代码和一些评论:
// Inserts first timer
Timer *newTimer = [NSEntityDescription insertNewObjectForEntityForName:@"Timer" inManagedObjectContext:myDataController.managedObjectContext];
// Inserts second timer
newTimer = [NSEntityDescription insertNewObjectForEntityForName:@"Timer" inManagedObjectContext:myDataController.managedObjectContext];
// Sets name of the second timer
newTimer.name = timerName;