本文介绍了Coredata - “NSObjectInaccessibleException-CoreData不能实现故障”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Core数据的新手,仍然弄清楚了螺母和螺栓,这个错误已经让我花了好几个小时,我似乎找不到一个解决方案。非常感谢任何帮助。



问题如下



我有两个视图,服务器并更新UI。我已经以这种方式设置流程



view1 - >从服务器发送HTTP请求 - 接收回调 - >将数据保存到Coredata - >从核心数据读取并显示在UI(回调和保存/读取Coredata发生在ViewController中)



view2 - >从服务器发送HTTP请求 - 接收回调 - >将数据保存到Coredata - >从核心数据读取并在UI上显示(回调和保存/读取Coredata发生在ViewController中)



查看2每3秒重复一次此过程,因为这是一个自动刷新屏幕。 >

问题是每当我试图在视图1和2之间切换真正快,它崩溃的应用程序与上述错误。如果我等待几秒钟在每个视图(等待数据从服务器获取),它工作正常。我做错了,我需要修改什么?

   - (void)refreshData {
[super refreshData ];
[[UserDataFactory sharedSingleton] refreshLoggedInUserDataAndRespondTo:self user:self.user];
}

- (BOOL)refreshDataCallback:(QExtendedHTTPOperation *)responseOperation {
[self saveToCoreData:responseOperation.responseArray];
NSMutableArray * tmp = [[NSMutableArray alloc] initWithArray:[self readFromCoreData]];
[self setData:tmp];
[tmp release];
[self.tableView reloadData];
return YES;
}

- (void)saveToCoreData:(NSArray *)responseArray {
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:@CoreView1inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError * error;
NSArray * items = [self.managedObjectContext executeFetchRequest:fetchRequest error:& error];
[fetchRequest release];

for(NSManagedObject * managedObject in items){
[self.managedObjectContext deleteObject:managedObject];
}

for(int i = 0; i CoreView1 * coreView1_ = [NSEntityDescription insertNewObjectForEntityForName:@CoreView1inManagedObjectContext:self。 managedObjectContext];
coreView_.id = [[responseArray objectAtIndex:i] id];
[self.managedObjectContext insertObject:coreView1_];
}
[self saveContext:self.managedObjectContext];
}

- (NSArray *)readFromCoreData {
NSEntityDescription * entity = [NSEntityDescription entityForName:@CoreView1inManagedObjectContext:self.managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError * error;
NSMutableArray * fetchedObjects = [[self.managedObjectContext executeFetchRequest:fetchRequest error:& error] mutableCopy];
[fetchRequest release];
return [fetchedObjects autorelease];
}

这是我使用的示例代码,即使View2有相同的回调并遵循相同的流程。



编辑1
忘记提及这一点,我总是得到saveToCoreData方法中的错误。另外,还要注意的是,如果我删除代码删除对象,它一切正常(我需要删除所有现有的数据,在保存更新的数据之前)。不确定发生了什么。

解决方案

删除您的删除代码使它工作的原因是因为您要删除持久化存储而不更新仍然具有与该数据相关联的管理对象实例仍在存储器中的另一视图。记住,当Core Data处理对象时,每个对象必须在数据库中有一行。当您删除该行时,Core Data会生气。



因此,为了解决这个问题,并且仍然删除您的数据,您应该让您的意见听取 NSManagedObjectContextWillSaveNotification 和/或 NSManagedObjectContextDidSaveNotification 通知并使用商店中最新的数据版本更新您的视图。在这一点上,你应该丢弃你的视图持有的任何Core Data对象,并从商店重新加载它们。


I am new to Core data and still figuring out the nuts and bolts and this error has been bugging me for hours and I cant seem to find a solution. Any help is greatly appreciated.

The problem is like this

I have two views which fetch data from the server and Update the UI. I have set up the flow this way

view1 -> Send HTTP Req from Server - Receive Callback -> Save Data To Coredata -> Read From Core Data and display on the UI (callback and saving/reading Coredata happen in ViewController)

view2 -> Send HTTP Req from Server - Receive Callback -> Save Data To Coredata -> Read From Core Data and display on the UI (callback and saving/reading Coredata happen in ViewController)

View 2 repeats this process every 3 seconds as this is a auto refresh screen.

The problem is whenever I try to switch between views 1 and 2 real fast, it crashes the app with above error. If I wait for a few seconds on each view (wait for data to be fetched from the server), it works fine. Am I doing something wrong, what do I need to modify?

- (void) refreshData {
    [super refreshData];
    [[UserDataFactory sharedSingleton] refreshLoggedInUserDataAndRespondTo:self user:self.user];
}

- (BOOL) refreshDataCallback:(QExtendedHTTPOperation*)responseOperation {
    [self saveToCoreData: responseOperation.responseArray];
    NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:[self readFromCoreData]];
    [self setData: tmp];
    [tmp release];
    [self.tableView reloadData];
    return YES;
}

- (void) saveToCoreData:(NSArray *) responseArray{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CoreView1" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    NSError *error;
    NSArray *items = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    [fetchRequest release];

    for (NSManagedObject *managedObject in items) {
            [self.managedObjectContext deleteObject:managedObject];
    }

    for (int i=0; i<[responseArray count]; i++) {
            CoreView1 *coreView1_ = [NSEntityDescription insertNewObjectForEntityForName:@"CoreView1" inManagedObjectContext:self.managedObjectContext];
            coreView_.id = [[responseArray objectAtIndex:i] id];
            [self.managedObjectContext insertObject:coreView1_];
    }
    [self saveContext:self.managedObjectContext];
}

- (NSArray *) readFromCoreData{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CoreView1" inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    NSError *error;
    NSMutableArray *fetchedObjects = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
    [fetchRequest release];
    return [fetchedObjects autorelease];
}

This is the sample code I'm using, even View2 has the same callbacks and follows the same flow.

Edit 1Forgot to mention this earlier, I always get the error in saveToCoreData method. Also, one more thing to note is that If I remove the code to delete objects it all works fine (I need to remove all the existing data from the table before I save the updated data). Not sure whats happening though.

解决方案

The reason removing your delete code makes it work is because you are removing the data in the persistent store without updating the other view that still has managed object instances tied to that data still in memory. Remember, while Core Data deals with objects, each object has to have a row in the database behind it. When you delete that row, Core Data gets angry.

So, to fix this problem, and still delete your data, you should have your views listen for NSManagedObjectContextWillSaveNotification and/or NSManagedObjectContextDidSaveNotification notifications and update your views with the most up to date versions of data in your store. It is at this point you should throw away any Core Data objects your views are holding onto, and reload them from the store.

这篇关于Coredata - “NSObjectInaccessibleException-CoreData不能实现故障”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:03