0

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

C00kieMonsta
  • 338
  • 3
  • 20
  • Have you tried doing what the warning suggests and put a break on warnBlockingOperationOnMainThread to see where it's occurring? Nevertheless, I suspect the problem is trying to set the photo in the background when the newGroup variable keeps changing. – Michael Mar 28 '16 at 02:30
  • Are you trying to persist the photo to CoreData? It won't work properly as it's loaded in the background, perhaps after you save the MOC. – Michael Mar 28 '16 at 02:36
  • Thanks for your help Michael, but could you be more precise about what you think I might do because I don't really get it. In fact what I am trying to do is to link all the users, being part of the group, that I added just in the code above this one, in the same view. Because I created a Many-To-Many relationship between Users and Groups. Each groups has many users and each user can have that many groups – C00kieMonsta Mar 28 '16 at 15:27

1 Answers1

0

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

Community
  • 1
  • 1
Michael
  • 8,891
  • 3
  • 29
  • 42
  • Thanks a lot for you help, I am currently working on the first point "warnBlockingOperationOnMainThread()", so for the rest I will see tonight. – C00kieMonsta Mar 29 '16 at 17:22
  • I don't know if by answering my Post, you got notified by the update of my question but as you can see below, I am still stuck with my problem. At least I managed to get rid of the "warnBlockingOperationOnMainThread()" but for the rest I am still working on it. So if you have some time left, I would be grateful if you could take a look at my code :p Thanks a lot – C00kieMonsta Mar 31 '16 at 07:18