English 中文(简体)
B. 重塑核心数据的问题
原标题:Problem with context in recreating CoreData

I need to delete all user info and recreate it. The code i use to create everything is in the app delegate:

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

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
    NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES) lastObject];
    NSURL *storeURL = [NSURL fileURLWithPath: [dir stringByAppendingPathComponent:
                                           @"Data.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;
}

然后,删除以下所有内容:

[context reset];

NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES) lastObject];
NSURL *storeURL = [NSURL fileURLWithPath: [dir stringByAppendingPathComponent:
                                           @"Data.sqlite"]];

NSError *error;
NSURL *storeURL = store.URL;
NSPersistentStoreCoordinator *storeCoordinator = [((CustomAppDelegate *)[[UIApplication sharedApplication] delegate]) persistentStoreCoordinator];

NSPersistentStore *store = [[storeCoordinator persistentStores] objectAtIndex:0];

[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];

[storeCoordinator release];
storeCoordinator = nil;

看来在工作上,问题在于,当时试图节省新的数据。 如果是这样的话:

NSManagedObjectContext *afterContext = [appDelegate managedObjectContext];
NSEntityDescription *userEntity = [NSEntityDescription entityForName:@"User"
                                              inManagedObjectContext:afterContext];
NSManagedObject *user = [NSEntityDescription
                         insertNewObjectForEntityForName:[userEntity name] inManagedObjectContext:afterContext];
[user setValue:@"John" forKey:@"firstName"];
if(![afterContext save:&error])
    NSLog(@"oops");

it crashes in afterContext save, it gaves me a EXC_BAD_INSTUCTION exception. When i do [appDelegate managedObjectContext] it creates the new persistentStore, so i don t know what could be the problem. A reference to a previous object? The stacktrace does not give any clue at all.

最佳回答

When i do [appDelegate managedObjectContext] it creates the new persistentStore

这样做是正确的,但只有不存在。 你不能“接收”你自己不拥有的物体,释放该物体,然后把它 set为零,并期望它工作。 你们为评估代表协调员设置了仓库协调员,这只是确定点值。 当你最终释放该协调员并安排该协调员时,上诉人仍然被安排到现在的被指派协调员的地址。 这是你坠毁的原因。 你们需要拿出一种方式,释放和确定实际应用的价值。 此外,评价员中管理的目标环境也需要更新,以反映变化。

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签