我的项目中有一个预定义和预加载的数据库,名为Database
,我将其定义如下:AppDelegate
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Database.sqlite"];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"]; //este es el que sirve!!! CREE ESTE
// NSLog(@"store URL %@", storeURL);
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:writableDBPath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Database" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:writableDBPath error:NULL];
}
}
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
在任何课程中,我都想从我使用的数据库中调用数据:
-(NSArray *)sqlCall {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"MotherWeekly" inManagedObjectContext:context];
NSFetchRequest *request = [[ NSFetchRequest alloc] init];
[request setFetchLimit:1];
[request setEntity:entityDesc];
NSString *a =[NSString stringWithFormat:@"%@==%d",@"id",pageNumber];
NSPredicate *pred = [NSPredicate predicateWithFormat:a];
[request setPredicate:pred];
NSError *error;
NSArray *objects = [context executeFetchRequest:request
error:&error];
[request release];
return objects ;
}
问题是:当我更新预加载数据库中的一个字段并在我的模拟器或 iPhone 上运行它时,数据似乎是旧的而不是更新的。
PS:在我注意到这个问题之前,我已经将我的应用程序提交到了 AppStore,所以我会拒绝我的二进制文件并使用工作/更新的数据库上传一个新版本,所以请帮助我
提前致谢