核心数据在两天后消失

时间:2014-06-30 16:08:07

标签: ios core-data

我正在使用核心数据来保存我的数据。前两天,它存储数据就好了。然而,两天后数据丢失了。我已经多次测试了这个倍数,结果相同。我错过了什么吗?有人有什么建议吗?下面是我的CoreDataStack类。非常感谢!

#import <Foundation/Foundation.h>

@interface CoreDataStack : NSObject

+(instancetype)defaultStack;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end

和实施文件

#import "CoreDataStack.h"

@implementation CoreDataStack


@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

+(instancetype)defaultStack{
    static CoreDataStack  *defaultStack;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        defaultStack = [[self alloc]init];
    });

    return defaultStack;
}

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

#pragma mark - Core Data stack

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Wen" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    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;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Wen.sqlite"];

    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;
}

#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
   NSLog(@"%@", url.absoluteString);
    return url;
}

@end

插入输入功能...忽略它说乳房:)它是一个婴儿测井应用

(void)insertLogEntry {
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
BreastEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"BreastEntry" inManagedObjectContext:coreDataStack.managedObjectContext];

/* Information i left out that is being stored */

[coreDataStack saveContext];

}

2 个答案:

答案 0 :(得分:0)

我认为这个应用程序一直打开吗? 您是否尝试过使用saveToPersistentStore:代替save:如果您销毁应用并重新启动它,您的数据是否会得到维护?

编辑:忽略saveToPersistentStore:评论,除非您使用的是RestKit。

答案 1 :(得分:0)

我确实继续找到了问题。它与排序描述符有关。我想按部分标题然后按行排序tableview。好吧,我只有一个排序描述符,它是为行。我认为这会抛弃排序,因为它试图用行键路径对这些部分进行排序,并且以某种方式使得tableview在X时间之后没有加载数据。感谢您的帮助。