0

我試圖從Parse獲取所有數據並將這些數據與我手機中的核心數據同步。我目前遇到一個沒有正確執行的提取請求的問題。我認爲原因是我正在爲不同的任務使用managedObjectContext,但我不知道如何解決這個問題。錯誤試圖獲取核心數據中的對象「 - [__ NSArrayI實體]:無法識別的選擇器發送到實例」

我在代碼中明確指出問題出在哪裏。這很奇怪,因爲我的代碼不會崩潰,但它只是向日志打印一個錯誤。 (我已經把顯示的日誌只是下面的代碼的結果)

這是我的代碼:

// 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. 
["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"] 
[email protected] 
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. 

因此,這是它,我認爲它與併發性有關,但不幸的是我找不到我的問題的答案。我一直在其他論壇上,但沒有人能提供正確的答案。

非常感謝

+0

您是否嘗試過做什麼的警告提示,並把休息的warnBlockingOperationOnMainThread上看到它的發生?不過,我懷疑這個問題是在newGroup變量不斷變化時試圖在後臺設置照片。 – Michael

+0

您是否試圖將照片保存到CoreData?它可能無法正常工作,因爲它在後臺加載,也許在保存MOC後。 – Michael

+0

感謝您的幫助邁克爾,但是您能否更準確地瞭解您認爲我可能會做的事情,因爲我沒有真正瞭解它。 實際上我想要做的就是鏈接所有用戶,作爲組的一部分,我只是在上面的代碼中添加了相同的視圖。因爲我在用戶和組之間創建了多對多關係。每個組都有很多用戶,每個用戶可以有很多組 – C00kieMonsta

回答

0

首先,它會是一個好主意,通過把一個突破上warnBlockingOperationOnMainThread()擺脫你解析警告。我無法在當前代碼中看到任何前景解析調用,因此它必須位於其他位置。 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

+0

非常感謝您的幫助,我目前正在研究第一個要點「warnBlockingOperationOnMainThread()」,因此剩下的我會在今晚看到。 – C00kieMonsta

+0

我不知道如果通過回覆我的帖子,你已經收到了我的問題更新通知,但正如你可以看到下面,我仍然堅持我的問​​題。至少我設法擺脫了「warnBlockingOperationOnMainThread()」,但其餘的我仍在努力。所以如果你還有一段時間,我會很感激你是否可以看看我的代碼:p非常感謝 – C00kieMonsta

相關問題