gpt4 book ai didi

objective-c - 核心数据和 NSEntityDescription

转载 作者:行者123 更新时间:2023-11-28 20:44:28 27 4
gpt4 key购买 nike

我的 iPhone 应用程序上的 Core Data 有问题。每次我尝试使用 NSEntityDescription 调用方法时,我都会得到一个 objc_exception_throw()(我在符号断点中设置的)。

我遇到异常的部分示例是:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
managedObjectContext = [favoritesFetchedResultsController managedObjectContext];
[request setEntity:[NSEntityDescription entityForName:@"FavoriteAddress" inManagedObjectContext:self.managedObjectContext]];
favorites = [managedObjectContext executeFetchRequest:request error:nil];

managedObjectContext = [recentsFetchedResultsController managedObjectContext];
[request setEntity:[NSEntityDescription entityForName:@"RecentAddress"
inManagedObjectContext:managedObjectContext]];
recents = [managedObjectContext executeFetchRequest:request error:nil];

[request release];

“FavoriteAddress”和“RecentAddress”是我的核心数据模型中对象的名称。 favoritesFetchedResultsControllerrecentsFetchedResultsController 都在以下方法中定义:

-(NSFetchedResultsController *)favoritesFetchedResultsController
{
NSLog(@"Favorites Fetched Results Controller called");
if(favoritesFetchedResultsController == nil)
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FavoriteAddress" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"FavoritesCache"];

aFetchedResultsController.delegate = self;
self.favoritesFetchedResultsController = aFetchedResultsController;

[aFetchedResultsController release];
[request release];

}

return favoritesFetchedResultsController;
}

-(NSFetchedResultsController *)recentsFetchedResultsController
{
NSLog(@"Recent Fetched Results Controller called");

if(recentsFetchedResultsController == nil)
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RecentAddress" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"RecentsCache"];
aFetchedResultsController.delegate = self;
self.recentsFetchedResultsController = aFetchedResultsController;

[aFetchedResultsController release];
[request release];
}

return recentsFetchedResultsController;
}

抛出异常时我得到的回溯是:

0  0x01703cb4 in objc_exception_throw ()
1 0x00fe6a87 in +[NSEntityDescription entityForName:inManagedObjectContext:] ()
2 0x0001ca7f in -[SavedAddressesViewController getData] (self=0x8173910, _cmd=0x22f72) at /Users/MasonSilber/Desktop/Programming Stuff/iOS Programming/XCode 4/StuffNearMe/StuffNearMe/SavedAddressesViewController.m:40
3 0x0001c93f in -[SavedAddressesViewController initWithStyle:] (self=0x8173910, _cmd=0x6bfe690, style=UITableViewStylePlain) at /Users/MasonSilber/Desktop/Programming Stuff/iOS Programming/XCode 4/StuffNearMe/StuffNearMe/SavedAddressesViewController.m:28
4 0x0039e3b6 in -[UITableViewController init] ()
5 0x00016662 in -[StartPageViewController goToSavedAddresses:] (self=0x815fde0, _cmd=0x2222a, sender=0x852a100) at /Users/MasonSilber/Desktop/Programming Stuff/iOS Programming/XCode 4/StuffNearMe/StuffNearMe/StartPageViewController.m:409
6 0x0156e969 in -[NSObject performSelector:withObject:withObject:] ()
7 0x00176429 in -[UIApplication sendAction:to:from:forEvent:] ()
8 0x003ae993 in -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] ()
9 0x0156e969 in -[NSObject performSelector:withObject:withObject:] ()
10 0x00176429 in -[UIApplication sendAction:to:from:forEvent:] ()
11 0x001763be in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
12 0x0021d45f in -[UIControl sendAction:to:forEvent:] ()
13 0x0021d928 in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
14 0x0021cbf3 in -[UIControl touchesEnded:withEvent:] ()
15 0x0019c7b4 in -[UIWindow _sendTouchesForEvent:] ()
16 0x0019c9dd in -[UIWindow sendEvent:] ()
17 0x001820c0 in -[UIApplication sendEvent:] ()
18 0x0017590c in _UIApplicationHandleEvent ()
19 0x01d96876 in PurpleEventCallback ()
20 0x0153fff5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
21 0x014a4902 in __CFRunLoopDoSource1 ()
22 0x014a31ea in __CFRunLoopRun ()
23 0x014a2694 in CFRunLoopRunSpecific ()
24 0x014a25ab in CFRunLoopRunInMode ()
25 0x01d94fdf in GSEventRunModal ()
26 0x01d950a4 in GSEventRun ()
27 0x00173851 in UIApplicationMain ()
28 0x000023ca in main (argc=1, argv=0xbffff5ec) at /Users/MasonSilber/Desktop/Programming Stuff/iOS Programming/XCode 4/StuffNearMe/StuffNearMe/main.m:16

我认为这可能与托管对象上下文有关(我还不太了解它们或核心数据),但我不确定。真的非常感谢任何帮助,我很迷茫,试图让它发挥作用。提前致谢。

最佳答案

您正在从 self.managedObjectContext 设置获取请求的 managedObjectContext 但您正在使用从每个获取结果 Controller managedObjectContext 属性返回的值执行获取请求。

这意味着您可能涉及两个或多个不同的上下文,或者其中一个上下文的值为 nil。由于您从 NSEntityDescription 调用中获得异常,因此 self.managedObjectContext 可能为 nil。

除非您出于某种原因明确拥有两个上下文,否则您应该只使用一个对上下文的引用。在这种情况下,您应该始终使用 self.managedObjectContext,注意它已正确填充。

关于objective-c - 核心数据和 NSEntityDescription,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977522/

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