我正在尝试使用FetchResultsController创建一个TableViewController。当我运行代码时,它会崩溃,指向下面显示的代码部分。这很奇怪,因为它几乎与我在教程中找到的相同。任何帮助表示赞赏
@interface favTable ()
@end
@implementation favTable
@synthesize managedObjectContext;
@synthesize fetchedResultsController;
- (NSFetchedResultsController *)fetchedResultsController {
if (self.fetchedResultsController != nil) {
return self.fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"FavoritesInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO],
[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO],
nil];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
self.fetchedResultsController.delegate = self;
return self.fetchedResultsController;
}
错误:
`frame #57963: 0x00006b73 EcoShoppingUI`-[favTable fetchedResultsController] + 51 at favTable.m:46`
CoreFoundation`CFRunLoopRunSpecific + 212
frame #58184: 0x003c8a1e UIKit`-[UIViewController view] + 184
frame #58185: 0x003c7fec UIKit`-[UIViewController nextResponder] + 34
frame #58186: 0x003eef1d UIKit`-[UIResponder _containsResponder:] + 40
frame #58187: 0x003d91cb UIKit`-[UINavigationController defaultFirstResponder] + 83
frame #58188: 0x003efdf1 UIKit`-[UIResponder(Internal) _deepestDefaultFirstResponder] + 36
frame #58189: 0x003efea9 UIKit`-[UIResponder(Internal) _promoteDeepestDefaultFirstResponder] + 36
frame #58190: 0x005e9508 UIKit`-[UIWindowController transitionViewDidStart:] + 89
frame #58191: 0x003a6401 UIKit`-[UITransitionView _didStartTransition] + 93
frame #58192: 0x003a708b UIKit`-[UITransitionView transition:fromView:toView:] + 1169
frame #58193: 0x005e8d6c UIKit`-[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 6114
frame #58194: 0x003cf857 UIKit`-[UIViewController presentViewController:withTransition:completion:] + 3579
frame #58195: 0x003cf9bc UIKit`-[UIViewController presentViewController:animated:completion:] + 112
frame #58196: 0x003cf9fc UIKit`-[UIViewController presentModalViewController:animated:] + 56
frame #58197: 0x00737f4a UIKit`-[UIStoryboardModalSegue perform] + 250
frame #58198: 0x0072c4d0 UIKit`-[UIStoryboardSegueTemplate perform:] + 174
frame #58199: 0x012c9e99 CoreFoundation`-[NSObject performSelector:withObject:withObject:] + 73
frame #58200: 0x0030414e UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
frame #58201: 0x00542a0e UIKit`-[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 145
frame #58202: 0x012c9e99 CoreFoundation`-[NSObject performSelector:withObject:withObject:] + 73
frame #58203: 0x0030414e UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
frame #58204: 0x003040e6 UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
frame #58205: 0x003aaade UIKit`-[UIControl sendAction:to:forEvent:] + 66
frame #58206: 0x003aafa7 UIKit`-[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
frame #58207: 0x003aa266 UIKit`-[UIControl touchesEnded:withEvent:] + 549
frame #58208: 0x003293c0 UIKit`-[UIWindow _sendTouchesForEvent:] + 513
frame #58209: 0x003295e6 UIKit`-[UIWindow sendEvent:] + 273
frame #58210: 0x0030fdc4 UIKit`-[UIApplication sendEvent:] + 464
frame #58211: 0x00303634 UIKit`_UIApplicationHandleEvent + 8196
frame #58212: 0x01f83ef5 GraphicsServices`PurpleEventCallback + 1274
frame #58213: 0x0129c195 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
frame #58214: 0x01200ff2 CoreFoundation`__CFRunLoopDoSource1 + 146
frame #58215: 0x011ff8da CoreFoundation`__CFRunLoopRun + 2218
frame #58216: 0x011fed84 CoreFoundation`CFRunLoopRunSpecific + 212
frame #58217: 0x011fec9b CoreFoundation`CFRunLoopRunInMode + 123
frame #58218: 0x01f827d8 GraphicsServices`GSEventRunModal + 190
frame #58219: 0x01f8288a GraphicsServices`GSEventRun + 103
frame #58220: 0x00301626 UIKit`UIApplicationMain + 1163
frame #58221: 0x00001ecd EcoShoppingUI`main + 141 at main.m:16
frame #58222: 0x00001e35 EcoShoppingUI`start + 53
答案 0 :(得分:1)
你有一个名为fetchedResultsController
的getter,你的getter调用self.fetchedResultsController;
,这意味着你递归调用它。
你需要以下列方式改变你的想法:
- (NSFetchedResultsController *)fetchedResultsController {
if (!_fetchedResultsController) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"FavoritesInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO],
[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO]]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
}
return _fetchedResultsController;
}