swift - Realm data Insertion took over 7mins for big size json -


i use alamofire download big json data around 7mb , use realmswift store data @ realmobject , swiftyjson parse.my realm object insertion after finished download json seems slow @ insertion.was wrong bad code?please guide me.

first of show simplest json :

{     {         "start" : "40000",         "end" : "1000000",         "name" : "smith",         "address" : "new york"     },{...},more 7000 records... } 

here alamofireapi protocol

import foundation import alamofire import swiftyjson  protocol requestdataapiprotocol{     func didsuccessdownloadingdata(results:json,statuscode : int)     func didfaildownloadingdata(err : nserror) }  class requestdataapi{  var delegate : requestdataapiprotocol init(delegate: requestdataapiprotocol){     self.delegate=delegate }  func post(requesturl:string,param:[string:string]){     alamofire.request(.get, requesturl, parameters: param)         .validate(statuscode: [200,500])         .responsejson(completionhandler: { (response: response<anyobject, nserror>) -> void in             if let error = response.result.error {                 self.delegate.didfaildownloadingdata(error)             } else if let jsonobject: anyobject = response.result.value {                 let json = json(jsonobject)                 self.delegate.didsuccessdownloadingdata(json,statuscode: (response.response?.statuscode)!)             }         }) }  func dorequestsitedata(token : string){     post(request_data,param:["data":"all","token":token]) }  } 

here realm db helper

import foundation import realmswift  class dbhelper{  func insertuserdata(list: userlist){     {         let realm = try! realm()         try realm.write({ () -> void in             realm.add(list,update: true)         })     } catch let error nserror {         print("insert error : \(error)")     } } } 

here realm modelobject

import foundation import realmswift  class userlist: object {       dynamic var start : string = ""      dynamic var end : string = ""      dynamic var name : string = ""      dynamic var address : string = ""  } 

and final code,view controller,

class viewcontroller : uiviewcontroller , requestdataapiprotocol{        var dbhelper = dbhelper()        var requestdataapi : requestdataapi!         override func viewdidload() {            super.viewdidload()            requestdataapi = requestdataapi(delegate : self)        }         override func viewdidappear(animated : bool){             //assume there 1 token request data             requestdataapi.dorequestsitedata(token)        }         func didsuccessdownloadingdata(results: json, statuscode: int){         dispatch_async(dispatch_get_main_queue(), {             print("downloaded json")             switch statuscode{             case 200 :                if results.count > 0{                    if let users = results.array {                       user in users{                          let userlist=userlist()                          userlist.start=user["start”].stringvalue                          userlist.end=user[“end”].stringvalue                          userlist.name=user[“name”].stringvalue                          userlist.address =user[“address”].stringvalue                          self.dbhelper.insertuserdata(userlist)                       }                    }                }                // took more 7 mins                print(“insertion done”)                break             case 500,401,400 :                //todo:             default : break            }         })     } } 

i know stupid describing code steps,i write simple working flow inserting json data realm swift.

i want know working flow or bad when handling many json data,and insertion.

the reason why asking data insertion took more 7 mins finish.

so,i need help,to make optimize @ code.

any guide?

update : use delegate , protocol requestdataapi learn style jamesqueue tutorial because beginner still learning swift.viewcontroller updated.that whole process detail,no more code left.editing question or answer new appreciated code optimizing.

insertuserdata method method opens transactions many times in loop. commit transaction little bit expensive operation.

can try put out open/commit transaction outside of loop? in other words, open transaction before entering loop, , commits transaction once after end of loop. following:

if results.count > 0 {     if let users = results.array {         let realm = try! realm()         try realm.write {             user in users{                 let userlist=userlist()                 userlist.start=user["start”].stringvalue                 userlist.end=user[“end”].stringvalue                 userlist.name=user[“name”].stringvalue                 userlist.address =user[“address”].stringvalue                 realm.add(userlist,update: true)             }         }     } } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -