Objectmapper: Thread and async problems...

Created on 7 Mar 2015  路  1Comment  路  Source: tristanhimmelman/ObjectMapper

Do you have any problems with threads or async tasks during the mapping process? I get a request and map the json via

let json = JSON(responseObject)
var array: Array = Array()
for (index: String, subJson: JSON) in json {
let data = Mapper().map(subJson.dictionaryObject!)
array.append(data)
}

During the mapping process, the ui on the main thread is blocked. Do you have any ideas what to do about that?

Most helpful comment

It is entirely possible that mapping is blocking the UI thread. ObjectMapper executes on the thread that it is called from so it is up to the developer make sure that it does not block the UI when executing.

You could do you mapping using Grand Central dispatch like this:

// Do Object mapping on a background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){

    let parsedObject = Mapper<T>().map(JSONResponse)        

    dispatch_async(dispatch_get_main_queue()){
        // use parsedObject on main thread
    }
}

>All comments

It is entirely possible that mapping is blocking the UI thread. ObjectMapper executes on the thread that it is called from so it is up to the developer make sure that it does not block the UI when executing.

You could do you mapping using Grand Central dispatch like this:

// Do Object mapping on a background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){

    let parsedObject = Mapper<T>().map(JSONResponse)        

    dispatch_async(dispatch_get_main_queue()){
        // use parsedObject on main thread
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

danyalaytekin picture danyalaytekin  路  4Comments

Dbigshooter picture Dbigshooter  路  4Comments

YunyueLin picture YunyueLin  路  3Comments

borut-t picture borut-t  路  4Comments

nashirox picture nashirox  路  4Comments