【发布时间】:2011-11-04 14:51:16
【问题描述】:
V。 iOS 5
我的 Model2.xcdatamodelId 包含 2 个实体。我的模型被命名为 2,因为我已经有一个名为 Model 的 Singleton 用于一些管理。
因此,我有 Model2.h 和 Model2.m。
我的问题:第一次,我的 Model2 是初始化的,我输入了一些默认数据,然后我提交。效果很好,它说我的 Model2 已正确保存。在我读取我的数据之后,数据从数据库中显示出来......所以它在数据库中是成功的。 但是.. 关闭并杀死我的应用时,我的应用似乎丢失了所有数据.. 并重新开始创建默认数据,因为它是空的...
提示:我认为我的问题可能与初始化有关。在这些行中:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; NSURL * storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: @"ProjectXYZ.db"]];
由于 ProjectXYZ.db 不存在,它应该创建它吗?...这是我迷路的部分..但它似乎适用于我从事过的另一个项目...:S
这是我的 Model2.h
#import <CoreData/CoreData.h> #import "Photos_Trophies.h" #import "Trophies.h"
@interface Model2 : NSObject
// High-level methods.
+ (void) commit;
...
// Object Retrieval
+ (NSArray*) trophies;
...
// Object Creation
+ (id) trophiesWithTitle:(NSString *)title;
@end
还有我的 Model2.m
#import "Model2.h" #import <UIKit/UIKit.h>
static NSManagedObjectContext * ctx;
@implementation Model2
+ (void) initialize {
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSURL * storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: @"ProjectXYZ.db"]];
NSError * error = nil;
ctx = [[NSManagedObjectContext alloc] init];
ctx.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
ctx.undoManager = [[NSUndoManager alloc] init];
if (![ctx.persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"%@", error);
}
//TEMPORARY... these are default trophies for example
if ([[Model2 trophies] count] == 0) {
[self trophiesWithTitle:@"Saved Trophy Test 1"];
[self trophiesWithTitle:@"Saved Trophy Test 2"];
[self trophiesWithTitle:@"Saved Trophy Test 3"];
[self commit];
}
}
+ (void) commit {
NSError* error = nil;
if (![ctx save:&error]) {
NSLog(@"ERREUR DANS COMMIT: %@", error.localizedDescription);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors)
NSLog(@"DetailedError: %@", [detailedError userInfo]);
} else
NSLog(@"%@", [error userInfo]);
} else
NSLog(@"Model2 SAVED");
}
+ (NSArray*) trophies {
NSFetchRequest* req = [[NSFetchRequest alloc] init];
req.entity = [NSEntityDescription entityForName:@"Trophies" inManagedObjectContext:ctx];
req.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"trophies_title" ascending:YES]];
NSError* error = nil;
NSArray* objects = [ctx executeFetchRequest:req error:&error];
return objects;
}
+ (id) trophiesWithTitle:(NSString *)title {
Trophies * trophies = [NSEntityDescription insertNewObjectForEntityForName:@"Trophies" inManagedObjectContext:ctx];
trophies.trophies_title = title;
return trophies;
}
【问题讨论】:
标签: iphone core-data ios5 nsmanagedobjectcontext