swift - 尝试获取核心数据中的对象时出错 "-[__NSArrayI entity] : unrecognized selector sent to instance"

标签 swift nsmanagedobjectcontext database-concurrency iphonecoredatarecipes

我正在尝试从 Parse 获取所有数据,并将这些数据与手机中的核心数据同步。我目前遇到一个提取请求未正确执行的小问题。我认为原因是我将 ManagedObjectContext 用于不同的任务,但我真的不知道如何解决这个问题。

我在代码中清楚地指出了问题发生的位置。这很奇怪,因为我的代码没有崩溃,但它只是将错误打印到日志中。 (我已将结果显示到代码下方的日志中)

这是我的代码:

// CORE DATA UPDATE

for var i = 0; i < self.messages.count; i++ {
let entity = NSEntityDescription.entityForName("Groups", inManagedObjectContext: self.managedObjectContext)
let newGroup = Groups(entity: entity!, insertIntoManagedObjectContext: self.managedObjectContext)

newGroup.groupId = self.messages[i].groupId
newGroup.updatedAt = NSDate()
newGroup.groupName = self.messages[i].groupName
self.messages[i].imageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
    if error == nil{
        newGroup.photo = imageData
    }else{
        print(error)
    }
})
newGroup.lastMessage = self.messages[i].message

let usersRelationship = newGroup.mutableSetValueForKey("Users")
let arrayMembers = self.dictionaryGroupIds[self.messages[i].groupId]! as [String]

print(arrayMembers)

for member in arrayMembers {

    print(member) 
    print("CODE IS STOPPING HERE ")
    // MY CODE STOPS HERE

    // When I want to print the members in the arraymembers, it never displays all the member, sometimes it is only one member than it is 3 members, but never all of them so

    let fetchRequest = NSFetchRequest(entityName: "Users")
    fetchRequest.predicate = NSPredicate(format: "username = %@", member)
    fetchRequest.returnsObjectsAsFaults = false

    do {
        let result = try self.managedObjectContext.executeFetchRequest(fetchRequest)
        usersRelationship.addObject(result)
    }catch{
        let fetchError = error as NSError
        print("\(fetchError), \(fetchError.userInfo)")
    }
}

do {
    // This get never printed to the log
    print("SUCCESS")
    try newGroup.managedObjectContext?.save()
} catch {
    let saveError = error as NSError
    print("\(saveError), \(saveError.userInfo)")
    print("CRASH")
       }
   }
}

这是日志中显示的内容:

2016-03-28 08:51:09.511 WeGrupp[1268:35749] Warning: A long-running operation is being executed on the main thread. 
 Break on warnBlockingOperationOnMainThread() to debug.
2016-03-28 08:51:09.823 WeGrupp[1268:35749] Warning: A long-running operation is being executed on the main thread. 
 Break on warnBlockingOperationOnMainThread() to debug.
["test1@info.com", "test2@info.com", "test3@info.com", "test4@info.com", "test5@info.com", "test6@info.com"]
te1t2@info.com
CODE IS STOPPING HERE
2016-03-28 08:51:10.140 WeGrupp[1268:35749] -[__NSArrayI entity]: unrecognized selector sent to instance 0x7fb0eb4d5c60
2016-03-28 08:51:10.143 WeGrupp[1268:35749] Warning: A long-running operation is being executed on the main thread. 
 Break on warnBlockingOperationOnMainThread() to debug.

就是这样,我认为这与并发有关,但不幸的是我找不到我的问题的答案。我去过其他论坛,但没有人能提供正确的答案。

非常感谢

最佳答案

首先,最好通过在 warnBlockingOperationOnMainThread() 上设置中断来消除解析警告。我在当前代码中看不到任何前台 Parse 调用,因此它一定在其他地方。 This SO question有一个答案展示了如何定义断点。

其次,以下代码值得关注:

self.messages[i].imageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
    if error == nil{
        newGroup.photo = imageData
    }else{
        print(error)
    }
})

newGroup 是一个 CoreData 实体,您将保存其 MOC,但同时也会在后台更新它。作为查看是否导致问题的简单第一步,请注释掉此代码。理想情况下,您只想在所有图像加载结束时保存 MOC 一次。

第三,您的代码崩溃了 - “无法识别的选择器发送到实例”。您可以通过中断所有异常来查看发生这种情况的位置。在断点导航器下执行此操作:

Add breakpoint

关于swift - 尝试获取核心数据中的对象时出错 "-[__NSArrayI entity] : unrecognized selector sent to instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36254572/

相关文章:

ios - NSFileManager fileExistsAtPath 在 segue(并且文件存在)之后为两个不同类中的相同路径返回 true 和 false

ios - 无法删除其他上下文中的对象

ios - 从子上下文分配对象时非法尝试建立关系

c# - 从多个线程修改 Entity Framework 实体

ios - 通过按钮生成随机图像

ios - 滚动时 UISwitch 正在更改其他单元格上的状态

ios - NSFetchedResultsController 仍然带回了已经被删除的 NSManagedObject

php - 时间戳困惑 : solution for concurrency issue

c# - 在不更新行版本的情况下检查实体的并发性

swift - 为什么我的 SKSpriteNode 可以工作,但只是暂时的