I am facing one exception when uploading a task. I am starting the process on the background and fetch core data records I do not know how can I violated Core Data’s concurrency model. Please check the below code.

```
//Call methods in background
    DispatchQueue.global(qos: .background).async {
        let onsourceNetwork = OnsourceNetworkUtility(withServiceRequestId: resources.value(forKey: "ServiceRequestId") as! String, inspectorId: resources.value(forKey: "InspectorId") as! String)
        onsourceNetwork?.delegate = self
        onsourceNetwork?.uploadResources()
    }

```

Got exception in this method.

```
//Fetch data from the core data
func fetchResourcesToBeUploaded(serviceRequestId: String, inspectorId: String) -> [ResourceMO] {
        let currentMOC = self.getCurrentContext()
        //upload status whether all chunks has been uploaded for a resource or not
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Resource")
        request.predicate = NSPredicate(format: "(serviceRequestId = %@ AND inspectorId = %@ AND uploadStatus = %@ OR uploadStatus = %@)", serviceRequestId, inspectorId, UploadStatus.Started.rawValue, UploadStatus.InProgress.rawValue)
        request.returnsObjectsAsFaults = false
        var resources = [ResourceMO]()
        do {
            resources = try currentMOC.fetch(request) as! [ResourceMO]// Core Data Violate here
        } catch {

            //print("Failed")
        }
        return resources;
    }

```

Please check image below for more details.
[![enter image description here](https://i.stack.imgur.com/HrXa8.png)](https://i.stack.imgur.com/HrXa8.png)
[![enter image description here](https://i.stack.imgur.com/iaNAv.png)](https://i.stack.imgur.com/iaNAv.png)

Here is the get contextCode:

```
func getCurrentContext() -> NSManagedObjectContext {
        var curMOC:NSManagedObjectContext? = self.managedObjectContext
        let thisThread:Thread = Thread.current
        if thisThread == Thread.main {
            if curMOC != nil {
                return curMOC!
            }
            let coordinator:NSPersistentStoreCoordinator? = self.persistentStoreCoordinator
            if coordinator != nil {
                curMOC = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)
                curMOC?.persistentStoreCoordinator = coordinator
            }
            return curMOC!
        }
        // if this is some other thread....
        // Get the current context from the same thread..
        var threadManagedObjectContext:NSManagedObjectContext? = thisThread.threadDictionary.object(forKey:"ManagedObjectContext_Key") as? NSManagedObjectContext;
        // Return separate MOC for each new thread
        if threadManagedObjectContext != nil {
            return threadManagedObjectContext!;
        }

        let coordinator:NSPersistentStoreCoordinator? = self.persistentStoreCoordinator
        if coordinator != nil {
            threadManagedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
            threadManagedObjectContext?.persistentStoreCoordinator = coordinator
            threadManagedObjectContext?.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
            threadManagedObjectContext?.undoManager = nil
            threadManagedObjectContext?.parent = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)
            thisThread.threadDictionary.setObject(threadManagedObjectContext!, forKey: "ManagedObjectContext_Key" as NSCopying)
            assert((threadManagedObjectContext != nil))
        }
        return threadManagedObjectContext!;
    }

```