2011-11-04 75 views
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; 
} 

回答

4

你的问题是这样的:NSInMemoryStoreType您需要使用持久的支持圣矿石。

做这样的事情:

NSURL *storeUrl = [NSURL fileURLWithPath:[self persistentStorePath]]; 

NSError *error = nil; 
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
               configuration:nil 
                 URL:storeUrl 
                options:options 
                 error:&error]) { 
    /* 
    Replace this implementation with code to handle the error appropriately. 

    abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

    Typical reasons for an error here include: 
    * The persistent store is not accessible 
    * The schema for the persistent store is incompatible with current managed object model 
    Check the error message to determine what the actual problem was. 
    */ 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    NSAssert(error == nil, @"error creating persistentStoreCoordinator"); 
} 
+2

我添加代码来解决你的问题。 – logancautrell

+0

什么是persistentStorePath的类型? 相同的persistentStoreCoordinator ...似乎错过了这些行之前的东西? – Marc

+0

哦!我实际上只是改变了你所做的商店类型,NSSQLiteStoreType和我添加的代码..它的工作原理是1. Thx对于那个提示很重要。非常感激 – Marc