Error trying to fetch objects in core data "-[__NSArrayI entity] : unrecognized selector sent to instance"

221 Views Asked by At

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.
["[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.

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

1

There are 1 best solutions below

2
Michael On

Firstly it would be a good idea to get rid of your Parse warnings by putting a break on warnBlockingOperationOnMainThread(). I can't see any foreground Parse calls in your current code, so it must be elsewhere. This SO question has an answer that shows how to define the breakpoint.

Secondly, the following code is a concern:

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

newGroup is a CoreData entity, and you're about to save its MOC, but you're also updating it in the background. As a simple first step to see if it's causing the problem, comment out this code. Ideally you want to save the MOC only once, at the end of all the image loads.

Thirdly, your code is crashing - "unrecognized selector sent to instance". You can see where this is occurring by breaking on all exceptions. Do this under the breakpoint navigator:

Add breakpoint