I am trying to get all the data from Parse and to sync this data with the Core Data from my phone. I currently encounter a little problem with a fetch request that is not executing correctly. I think the cause is that I am using the managedObjectContext for different task, but I don't really know how to fix this.
I indicate clearly in my code where the problem occurs. And it is quite weird because my code does not crash but it just prints an error to the log. (I have put the results displaying to the log just below the code)
This is my code:
// 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")
}
}
}
This is what gets displayed to the log:
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.
So this is it, I think it has something to do with Concurrency but I could not find an answer to my question unfortunately. I have been on other forums but nobody could provide a proper answer.
Many thanks