ios - FetchResultsController 崩溃

标签 ios uitableview nsfetchedresultscontroller nsfetchrequest

我正在尝试使用 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

最佳答案

您有一个名为 fetchedResultsController 的 getter,并且您的 getter 调用了 self.fetchedResultsController;,这意味着您可以递归地调用它。

您需要按以下方式更改您的 getter:

- (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;

}

关于ios - FetchResultsController 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24913507/

相关文章:

c++ - 如何用指数声明定义

ios - UITableView 部分索引针对 iOS 7 中的错误单元格,顶行隐藏在导航栏下方

iphone - 如何在 UITableView 中用图像填充一行

ios - 无法重新加载 UITableView

ios - 是否可以排除具有最新日期的对象?

ios - Swift:将 xib 添加到 UIView

ios - 如何使用标识符将 NSLayoutConstraint 访问到 tableviewcell 中?

ios - UITableView 单元格约束问题 - Swift

objective-c - 分割获取的结果 Controller 中断搜索显示

ios - Core Data 中多对多关系的 sectionNameKeyPath 值