1
五,iOS 5核心數據不支持iOS 5
我有我的Model2.xcdatamodelId包含2個實體。我的模型被命名爲2,因爲我已經有一個名爲Model的Singleton用於某些管理。
因此,我有Model2.h和Model2.m。
我的問題:第一次,我的模型2初始化,我把一些默認數據,然後我提交。它工作的很好,它說我的Model2已經被正確保存了。在我正在閱讀我的數據之後,數據從數據庫中顯示出來,所以它在數據庫中是成功的。 但是,當關閉並殺死我的應用程序,我的應用程序似乎已經失去了所有數據..並重新創建默認數據,因爲它是空的...
提示:我想我的問題可能大約初始化.. whithin這些線路:
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;
}
我添加代碼來解決你的問題。 – logancautrell
什麼是persistentStorePath的類型? 相同的persistentStoreCoordinator ...似乎錯過了這些行之前的東西? – Marc
哦!我實際上只是改變了你所做的商店類型,NSSQLiteStoreType和我添加的代碼..它的工作原理是1. Thx對於那個提示很重要。非常感激 – Marc