0

I searched a lot for this error and was not able to find a solution.

I have my managedObject:

lazy var managedObjectContext: NSManagedObjectContext = {
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

and I have a Singleton class

lazy var managedObjectContext : NSManagedObjectContext? = {

    if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
            return appDelegate.managedObjectContext
    }
    return nil

}()

I have a method which is called lot of time whenever I update / delete / load data from database

class func LoadDatabase(tableName: String, predicateString: String, predicateIntValue: Int) -> [AnyObject]? {

        do {
            let managedObjectContext = Singleton.sharedInstance.managedObjectContext

            if managedObjectContext == nil {
                return nil
            }

            let fReq: NSFetchRequest = NSFetchRequest(entityName: tableName)
            if predicateString != "" {
                fReq.predicate = NSPredicate(format: predicateString,  predicateIntValue)
            }

            var result: [AnyObject]
            do {
                if managedObjectContext == nil {
                    return nil
                }
                result = try managedObjectContext!.executeFetchRequest(fReq)
            } catch {
                return nil
            }

            // update exist event
            if
                result.count != 0 {
                return result
            }
        } catch let error1 as NSError {
            print("No values to load for table: \(error1)")
        }
        return nil
    }

I think I have problem on concurrence access. When the app starts it stars asynchronous requests to update the database. But I also call the method LoadDatabase from varius controller. Once the asynchronous call done with updating the datable, it call another method to delete datas which are not more in remote database.

This is what I get. This does not happens always. ESC_BAD_ACCESS Error

I tried to change to MainQueueConcurrency but now I am getting also this error:

2016-03-20 21:27:01.371 Tamil League[1070:245784] CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)
2016-03-20 21:27:01.371 Tamil League[1070:245784] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack:
(0x24a9e2eb 0x2426adff 0x24a9e231 0x249e7fe5 0x261eb683 0x261ea063 0x261e2dfb 0x24a60b21 0x24a5ee17 0x24a5f255 0x249b1bb9 0x249b19ad 0x25c2baf9 0x28c9dfb5 0xa6d5c 0x24664873)
libc++abi.dylib: terminating with uncaught exception of type NSException

Does anyone know how to solve this?

Vannian
  • 1,490
  • 5
  • 14
  • 26
  • Why do you declare your `managedObjectContext` as optional although the Core Data context is clearly non-optional and you know also at design time that the AppDelegate class exists? – vadian Mar 20 '16 at 17:31
  • lazy var managedObjectContext : NSManagedObjectContext? = {... So I can control if it is null or not. – Vannian Mar 20 '16 at 17:35
  • `return nil` will never be reached because the `AppDelegate` class is required to exist. That's a good example of a complete useless optional binding. – vadian Mar 20 '16 at 17:37
  • Ok. I removed it. I thought it was nil, thats why I changed. However the error continues to happen – Vannian Mar 20 '16 at 17:42
  • Since the `NSPredicate` instance is created dynamically and can be `nil` I'd check for that. – vadian Mar 20 '16 at 17:45
  • I think the problem is Signleton class. I changed it to normal class, but no getting another error... – Vannian Apr 01 '16 at 17:25

0 Answers0