OGeek|极客世界-中国程序员成长平台

标题: ios - Core Data iOS 应用程序在获取上下文时崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:51
标题: ios - Core Data iOS 应用程序在获取上下文时崩溃

我将 Quickblox 用于我的聊天应用程序,并使用核心数据来存储消息历史记录。

当我登录聊天时,我重新发送了上次发送失败的消息。 (即我从 Core Data 获取消息并获取未发送的消息)

有时它可以工作,但有时应用程序在辅助方法(用于获取核心数据上下文)上崩溃:

+ (NSManagedObjectContext *)context {
    return ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
}

我没有对 App Delegate 进行任何更改,它只是一个普通的启用 CoreData 的 AppDelegate:

@interface AppDelegate : UIResponder <UIApplicationDelegate, QBActionStatusDelegate, QBChatDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;


@end

这就是我在 DBHelper 类中检索消息历史记录的方式

+ (NSMutableArray *)fetchMessagesWithSenderIDNSString *)userID {

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName"Message" inManagedObjectContext:[DBHelper context]]; //the app crashes here, inside [DBHelper context]
    [fetchRequest setEntity:entity];

    NSPredicate *pred = [NSPredicate predicateWithFormat"sender.userID = %@", userID];
    [fetchRequest setPredicate:pred];


    NSError *error;

    NSMutableArray *fetchedObjects = [[DBHelper context] executeFetchRequest:fetchRequest error:&error].mutableCopy;
    return fetchedObjects;
}

日志:

2014-01-05 16:32:05.365 Chat[1517:60b] completedWithResult: <QBMRegisterSubscriptionTaskResult: 0x14e955e0>
2014-01-05 16:32:05.366 Chat[1517:60b] error: (
    "Invalid provisioning profiles. You have to use valid Provisioning Profiles for your project"
)
2014-01-05 16:32:06.549 Chat[1517:180f] QBChat/didConnect
2014-01-05 16:32:07.913 Chat[1517:4103] -[QBChat xmppStreamDidAuthenticate:] -> user: 573782, supportsStartTLS: 1, isSecure: 0
2014-01-05 16:32:07.913 Chat[1517:60b] chatDidLogin
2014-01-05 16:32:07.916 Chat[1517:60b] -[QBMGetTokenPerformer managedObjectContext]: unrecognized selector sent to instance 0x14d48800
(lldb) 

enter image description here

enter image description here



Best Answer-推荐答案


-[QBMGetTokenPerformer managedObjectContext]: unrecognized selector sent to instance 0x14d48800

这行意味着你调用了 QBMGetTokenPerformer 类的对象的 managedObjectContext 方法,它不能识别该方法。

从您问题中的代码和您显示的屏幕截图来看,这不应该发生 - 我不知道对应用程序委托(delegate)的调用如何有时返回一个不相关的对象 -我唯一一次看到这种情况发生是由于内存管理问题,但应用程序委托(delegate)实际上是一个单例,因此无法重新分配其内存。

我只能建议您向您的应用程序添加一个异常断点 - 尽管看起来您已经有了一个。您也许可以将您的方法分成单独的行,以便您可以看到每一行发生了什么。

关于ios - Core Data iOS 应用程序在获取上下文时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931686/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4