核心数据 - 数据故障

时间:2014-02-09 13:35:02

标签: ios objective-c core-data save

我正在将数据保存到核心数据,更新,删除等。 关于核心数据和核心数据我完全没有错误。保存。 但是当我尝试打开sqlite数据库文件时,没有任何内容,也没有更新并保存到核心数据。

的AppDelegate:

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

// 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:@"CoreDataModel" 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:@"CoreDataModel.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
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

更新核心数据的示例:

- (void)enableORdisableAlertInDatabase:(FMDatabase*)database andOperation:(int)operation andTrapID:(int)trapID
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:kCORE_DATA_TRAP_ENTITY inManagedObjectContext:appDelegate.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"trapID==%i", trapID];
    [fetchRequest setPredicate:predicate];

    NSError *error = nil;
    NSArray *fetchedObjects = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
    }
    else {
        CoreDataTrap *trap = (CoreDataTrap*)fetchedObjects.firstObject;
        [trap setAlert:[NSNumber numberWithInt:operation]];

        [appDelegate.managedObjectContext save:&error];

        if (error != nil) {
            NSLog(@"enableORdisableAlertInDatabase Error: %@", error.localizedDescription);
        }
    }
}

修改 这是添加或更新对象的函数:

- (void)addOrUpdateTrap:(Traps*)trapObject
{
    int trapID = trapObject.getTrapID;
    CoreDataTrap *trapEntity = nil;
    NSError *error = nil;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"CoreDataTrap"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"trapID == %d", trapID];
    [fetchRequest setPredicate:predicate];

    NSArray *results = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (results == nil)
    {
        // Handle error
    }
    else if ([results count] == 0)
    {
        // Nothing to update, add new trap
        // Create a new record (row)
        trapEntity = [NSEntityDescription insertNewObjectForEntityForName:@"CoreDataTrap" inManagedObjectContext:appDelegate.managedObjectContext];
    }
    else
    {
        trapEntity = results[0]; // There should be only one object for the ID.
    }


    if (trapEntity != nil)
    {
        // Set properties for new or existing object ...

        // Int
        [trapEntity setTrapID:[NSNumber numberWithInt:trapObject.getTrapID]];
        [trapEntity setType:[NSNumber numberWithInt:trapObject.getTrapType]];
        [trapEntity setDist:[NSNumber numberWithInt:trapObject.getTrapDistanceToCar]];
        [trapEntity setDist_to_close_point:[NSNumber numberWithInt:trapObject.getTrapDistanceToClosePoint]];
        [trapEntity setActive:[NSNumber numberWithInt:trapObject.isActive]];
        [trapEntity setAlert:[NSNumber numberWithInt:trapObject.isAlert]];
        [trapEntity setAlarmDistance:[NSNumber numberWithInt:trapObject.alarmDistance]];
        [trapEntity setRoadNumber:[NSNumber numberWithInt:trapObject.roadNumber]];
        [trapEntity setPolys:[NSNumber numberWithInt:trapObject.polygons]];
        [trapEntity setEnter_to_area:[NSNumber numberWithInt:trapObject.getTrapEnterToArea]];

        // Double
        [trapEntity setLat:[NSNumber numberWithDouble:trapObject.getTrapLat]];
        [trapEntity setLon:[NSNumber numberWithDouble:trapObject.getTrapLon]];
        [trapEntity setClose_point_lat:[NSNumber numberWithDouble:trapObject.getTrapClosePointLat]];
        [trapEntity setClose_point_lon:[NSNumber numberWithDouble:trapObject.getTrapClosePointLon]];

        // NSString
        [trapEntity setLastTrapAlarm:[NSString stringWithFormat:@"%li", trapObject.getTrapLastAlarm]];
        [trapEntity setPoly0:trapObject.getTrapPolygonA];
        [trapEntity setPoly1: trapObject.getTrapPolygonB];
        [trapEntity setPoly2: trapObject.getTrapPolygonC];
        [trapEntity setPolygonAzimut1: trapObject.getTrapPolygonAzimuthA];
        [trapEntity setPolygonAzimut2: trapObject.getTrapPolygonAzimuthB];
        [trapEntity setPolygonAzimut3: trapObject.getTrapPolygonAzimuthC];
        [trapEntity setDesc: trapObject.getTrapDesc];
        // etc. for all properties ...

        error = nil;
        if (![appDelegate.managedObjectContext save:&error])
        {
            NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
        }
    }
}

更新 - 1: 我记录了这些对象并得到了这个(当我将它们添加到核心数据中时):

"<CoreDataTrap: 0x147c4140> (entity: CoreDataTrap; id: 0x1477d510 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p11> ; data: <fault>)",
    "<CoreDataTrap: 0x147c4240> (entity: CoreDataTrap; id: 0x1477d520 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p12> ; data: <fault>)",
    "<CoreDataTrap: 0x145507d0> (entity: CoreDataTrap; id: 0x147cd410 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p13> ; data: <fault>)",
    "<CoreDataTrap: 0x14550900> (entity: CoreDataTrap; id: 0x147cd420 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p14> ; data: <fault>)",
    "<CoreDataTrap: 0x14550a30> (entity: CoreDataTrap; id: 0x147cd430 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p15> ; data: <fault>)",
    "<CoreDataTrap: 0x145401b0> (entity: CoreDataTrap; id: 0x147cd440 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p16> ; data: <fault>)",
    "<CoreDataTrap: 0x145402b0> (entity: CoreDataTrap; id: 0x147cd450 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p17> ; data: <fault>)",
    "<CoreDataTrap: 0x14540440> (entity: CoreDataTrap; id: 0x147cd460 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p18> ; data: <fault>)",
    "<CoreDataTrap: 0x1453e380> (entity: CoreDataTrap; id: 0x147cd470 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p19> ; data: <fault>)",
    "<CoreDataTrap: 0x1453e480> (entity: CoreDataTrap; id: 0x147cd480 <x-coredata://CD85E666-FA17-4D5F-A221-9BC962C7667B/CoreDataTrap/p20> ; data: <fault>)"

我也记录了他们的价值,似乎是正确的价值。

仍然没有更新。

2 个答案:

答案 0 :(得分:4)

你怎么知道数据不在SQLite中?您是在查看文件还是在屏幕上看不到数据?那里有两个不同的潜在问题。

如果您正在查看该文件并且您使用的是iOS 7,那么您可能正在考虑将日记作为您混淆的根源。日记帐模式在iOS7中打开,将导致更新在辅助文件中保留一段时间。这将使您看到数据不在SQLite文件中,因为它们尚未从日志文件移动到SQLite文件。在这种情况下,一切都很好。

如果您要查看要在视图中显示的数据,那么您的视图代码可能有问题。发布该视图代码将有助于诊断它。

我怀疑你正在查看SQLite文件。如果我错了,请纠正我。

顺便说一下,这个:

[appDelegate.managedObjectContext save:&error];

if (error != nil) {
  NSLog(@"enableORdisableAlertInDatabase Error: %@", error.localizedDescription);
}

检查此错误是一种不好的方法。 -save:返回一个布尔值,应该查看错误:

if ([[appDelegate managedObjectContext] save:&error] == NO) {
  NSLog(@"Error saving: %@\n%@", [error localizedDescription], [error userInfo]);
}

答案 1 :(得分:0)

使用最新版本的Base或SQLite Professional来检查您的数据库,你会没事的。