一、如果想把模拟其中保存的数据在真机中显示出来,可以在 AppDelegate 里用下面方法:
模拟器保存完数据,然后进入目录:“用户名/Library/Application Support/iPhone Simulator/4.2/Applications/" 目录下,找到 p.sqlite,复制到软件的工程目录里。然后导入到工程里面,这样可以用模拟器里面保存的数据了。
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return managedObjectModel;
}
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"p.sqlite"];
/*
Set up the store.
For the sake of illustration, provide a pre-populated default store.
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"p" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSError *error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
return persistentStoreCoordinator;
}
//Returns the path to the application's documents directory.
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
二、常见错误 entityForName: could not locate an NSManagedObjectModel for entity name "Entity" 的解决方法:
如果是如果是 AppDelegate.m 里面出现错误,需要这么写
    FavoriteViewController *favoriteVC = [[FavoriteViewController alloc] initWithNibName:nil bundle:nil];
    favoriteVC.managedObjectContext = self.managedObjectContext;
如果是两个界面跳转,主要是出现在 iOS 4.0 以下版本,正确写法是:
Test.h
NSManagedObjectContext *managedObjectContext;
NextTest *next;
Test.m
next.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController: next animated:YES];
错误写法:
NextTest *next = [[NextTest alloc] init];
next.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController: next animated:YES];
[next release];
三、条件过滤:
选择 cid=1 的数据
- (NSFetchedResultsController *)fetchedResultsController {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cid=1"];