【问题标题】:app crashing while fetching record from core data从核心数据中获取记录时应用程序崩溃
【发布时间】:2014-12-01 16:18:59
【问题描述】:

朋友们,我的应用程序在核心数据中执行获取请求时崩溃了。以下是崩溃时显示的消息。

“2014-10-07 14:38:07.203 Social[1540:60b] -[AppDelegate fetchAllAds]:无法识别的选择器发送到实例 0x9725da0 2014-10-07 14:38:07.209 Social[1540:60b] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,**原因:'-[AppDelegate fetchAllAds]: 无法识别的选择器发送到实例0x9725da0' *** 首先抛出调用栈:

(
0   CoreFoundation                      0x025201e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x01e1e8e5 objc_exception_throw + 44
2   CoreFoundation                      0x025bd243 -[NSObject(NSObject)  
doesNotRecognizeSelector:] + 275
3   CoreFoundation                      0x0251050b ___forwarding___ + 1019
4   CoreFoundation                      0x025100ee _CF_forwarding_prep_0 + 14
5   Social                              0x00002e5b -[ShoppingCartViewController viewDidLoad] +  
139
6   UIKit                               0x00bfd33d -[UIViewController loadViewIfRequired] + 696
7   UIKit                               0x00bfd5d9 -[UIViewController view] + 35
8   UIKit                               0x00c0cf89 -[UIViewController shouldAutorotate] + 36
9   UIKit                               0x00c0d2d1 -[UIViewController _preferredInterfaceOrientationForPresentationInWindow:fromInterfaceOrientation:] + 297
10  UIKit                               0x00eab3d5 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:] + 2330
11  UIKit                               0x00c095d5 -[UIViewController presentViewController:withTransition:completion:] + 6538
12  UIKit                               0x00c09aef -[UIViewController presentViewController:animated:completion:] + 130
13  UIKit                               0x00c09b2f -[UIViewController presentModalViewController:animated:] + 56
14  UIKit                               0x01053e00 -[UIStoryboardModalSegue perform] + 271
15  UIKit                               0x01042f0c -[UIStoryboardSegueTemplate _perform:] + 174
16  UIKit                               0x01042f87 -[UIStoryboardSegueTemplate perform:] + 115
17  libobjc.A.dylib                     0x01e30880 -[NSObject performSelector:withObject:withObject:] + 77
18  UIKit                               0x00ae03b9 -[UIApplication sendAction:to:from:forEvent:] + 108
19  UIKit                               0x00ae0345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
20  UIKit                               0x00be1bd1 -[UIControl sendAction:to:forEvent:] + 66
21  UIKit                               0x00be1fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
22  UIKit                               0x00be1243 -[UIControl touchesEnded:withEvent:] + 641
23  UIKit                               0x00b1fddd -[UIWindow _sendTouchesForEvent:] + 852
24  UIKit                               0x00b209d1 -[UIWindow sendEvent:] + 1117
25  UIKit                               0x00af25f2 -[UIApplication sendEvent:] + 242
26  UIKit                               0x00adc353 _UIApplicationHandleEventQueue + 11455
27  CoreFoundation                      0x024a977f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
28  CoreFoundation                      0x024a910b __CFRunLoopDoSources0 + 235
29  CoreFoundation                      0x024c61ae __CFRunLoopRun + 910
30  CoreFoundation                      0x024c59d3 CFRunLoopRunSpecific + 467
31  CoreFoundation                      0x024c57eb CFRunLoopRunInMode + 123
32  GraphicsServices                    0x03ca25ee GSEventRunModal + 192
33  GraphicsServices                    0x03ca242b GSEventRun + 104
34  UIKit                               0x00adef9b UIApplicationMain + 1225
35  Social                              0x00017a2d main + 141
36  libdyld.dylib                       0x0393b701 start + 1
37  ???                                 0x00000001 0x0 + 1

) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

以下是我的代码。

- (void)viewDidLoad
{
    DBManager* manager = [UIApplication sharedApplication].delegate;
    self.fetchedRecordsArray = [manager fetchAllAds];
    [self.shoppingtbl reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.fetchedRecordsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
    *)indexPath
{    
    static NSString *CellIdentifier = @"ShoppingCart";
    ShoppingCart *cell = (ShoppingCart*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier 
    forIndexPath:indexPath];
    Ad * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
    cell.addesc.text = [NSString stringWithFormat:@"%@",record.data];
    return cell;
}

请注意,我已经在名为 dbmanager 的单独文件中停用了所有核心数据功能。

【问题讨论】:

    标签: ios uitableview core-data ios7 xcode5


    【解决方案1】:

    它告诉你AppDelegate 没有fetchAllAds 的功能。

    就像一个简短的说明。它不应该有这个功能。

    应用委托中缺少某些内容(或输入错误)。如果您无法修复它,那么您需要在您的应用委托中显示代码。

    编辑

    哦!我才意识到。您有一个名为 DBManager 的类,但您加载它的方式...

    DBManager* manager = [UIApplication sharedApplication].delegate;
    

    实际上是将UIAppDelegate 放入属性中。

    你需要...

    DBManager *manager = [[DBManager alloc] init];
    

    这将停止错误。

    这是在做什么……

    当您运行 [manager fetchAllAds]; 时,它会获取属性 manager

    这是对 App Delegate 的引用,因为您加载它的方式。

    编辑 1

    好的,听起来您的 DBManager 可能是您的应用委托的属性。

    那你就可以这样加载了……

    DBManager *manager = [[UIApplication sharedApplication] delegate].dbManager; //or whatever the property is.
    

    【讨论】:

    • 在我的代码中,我没有指定 [appdelegate fetch ads]...但显示了我能做什么。
    • @user3354840 是的,但是你有 DBManager *manager 并且你将 app delegate 放入其中。然后,当您尝试运行 fetchAllAds 时,它不存在。您需要创建DBManager 并将其放入manager 属性中。
    • 现在应用程序正在运行...但是所有字段都显示为空..我该怎么办..但是数据保存在数据库中。
    • @user3354840 如果这不起作用,那么我不看更多代码就知道了。
    【解决方案2】:

    朋友,这不是 coreData 的问题,但您在 viewDidLoad 处访问了错误的对象

    - (void)viewDidLoad
    {
    DBManager* manager = [UIApplication sharedApplication].delegate;
    self.fetchedRecordsArray = [manager fetchAllAds];
    [self.shoppingtbl reloadData];
    }
    

    在这里,您的代码 [UIApplication sharedApplication].delegate; 将返回 appDelegate 而不是 DBManager 的对象,并且您将其存储在 DBManager 中,这是错误的。因此,如果您的 appDelegate 中有属性 dbManager,请以这种方式访问​​ [[[UIApplication sharedApplication] delegate] dbManager]

    因此,如果您的应用中有 DBManager 的属性,请以这种方式访问​​它

    - (void)viewDidLoad
        {
        DBManager* manager = [[[UIApplication sharedApplication] delegate] dbManager];
        self.fetchedRecordsArray = [manager fetchAllAds];
        [self.shoppingtbl reloadData];
        }
    

    或者在这里新建一个对象

    - (void)viewDidLoad
        {
        DBManager* manager = [[DBManager alloc] init];
        self.fetchedRecordsArray = [manager fetchAllAds];
        [self.shoppingtbl reloadData];
        }
    

    【讨论】:

    • 我现在正在工作,谢谢..但所有字段都显示为空..我该怎么办..
    • @user3354840 那么您是否创建了一个新对象或从 appDelegate 访问该属性?
    • 如果您正在创建一个新的类对象,那么您还应该在您的 fetchAllAds 方法或 DBManager 类的 viewDidLoad 中创建新的 managedObject 上下文并创建一个新的获取请求。只有这样它才会起作用。
    • ok 如果调用 DBManager* manager = [[[UIApplication sharedApplication] delegate] dbManager];方法。那么它会工作吗?
    • @user3354840 兄弟,简单的事情就是这样,从项目中的任何地方访问 DBManager 的对象,但它应该有一个有效的 NSManagedObjectContext 对象和 NSFetchRequest 对象来访问来自 coreData 的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    相关资源
    最近更新 更多