gpt4 book ai didi

ios - 核心数据-数据故障

转载 作者:行者123 更新时间:2023-11-28 19:04:35 24 4
gpt4 key购买 nike

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

应用委托(delegate):

- (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>)"

我还记录了它们的值,它似乎是正确的值。

仍然没有更新。

最佳答案

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

如果您正在查看文件并且您使用的是 iOS 7,那么您可能正在查看日记是您困惑的根源。日志模式在 iOS7 中打开,将导致更新在辅助文件中放置一段时间。这会给你数据不在 SQLite 文件中的外观,因为它们还没有从日志文件移动到 SQLite 文件。在这种情况下,一切都很好。

如果您正在寻找要出现在 View 中的数据,那么您的 View 代码可能有问题。发布该 View 代码将有助于对其进行诊断。

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

顺便说一句,这个:

[appDelegate.managedObjectContext save:&error];

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

检查此错误的方法很糟糕。 -save: 返回一个 bool 值,如果有错误应该查询:

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

关于ios - 核心数据-数据故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21659667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com