json - How to insert bulk data into SQLite database in iOS using swift -


i want insert more 20000 records local database sqlite using swift.i getting data json service.i able insert records successfully, taking morethan 5 minutes insert.i have searched lot , implemented begin , commit transactions, have not found change in execution time. below code:

func saveteamdepartmentvaluedata(teamdepartmentvaluearray : nsmutablearray) {     var insertteamdepartmentvaluedataquery : string = string()      if (teamdepartmentvaluearray.count > 0) {         var errmsg:unsafemutablepointer<int8> = nil         sqlite3_exec(database, "begin transaction", nil, nil, &errmsg)          insertteamdepartmentvaluedataquery = "insert or ignore store_feedback_form_kpi (team_id, department_id,value_id,ordinal,sub_category_id,team_department_value_id) values (?, ?, ?, ?, ?, ?)"          println("insertteamdepartmentvaluedataquery \(insertteamdepartmentvaluedataquery)")         var csql = insertteamdepartmentvaluedataquery.cstringusingencoding(nsutf8stringencoding)         var result:cint=0         var statement:copaquepointer = nil         var path = getpath()         var dbpath = path.cstringusingencoding(nsutf8stringencoding)         let check = sqlite3_open(dbpath!, &database)         var teamdepartmentvaluedict : nsdictionary = nsdictionary()          sqlite3_prepare_v2(database, csql!, -1, &statement, nil);          teamdepartmentvaluedict in teamdepartmentvaluearray         {             var teamdepartmentvaluedata : teamdepartmentvalue = teamdepartmentvalue ()             if var teamid = teamdepartmentvaluedict.valueforkey("team_id") as? string             {                 teamdepartmentvaluedata.teamid = teamid.toint()!             }             else{                 teamdepartmentvaluedata.teamid = 0             }             if var departmentid = teamdepartmentvaluedict.valueforkey("department_id") as? string             {                 teamdepartmentvaluedata.departmentid = departmentid.toint()!             }             else{                 teamdepartmentvaluedata.departmentid = 0             }             if var valueid = teamdepartmentvaluedict.valueforkey("value_id") as? string             {                 teamdepartmentvaluedata.valueid = valueid.toint()!             }             else{                 teamdepartmentvaluedata.valueid = 0             }             if var ordinal = teamdepartmentvaluedict.valueforkey("ordinal") as? string             {                 teamdepartmentvaluedata.ordinal = ordinal.toint()!             }             else{                 teamdepartmentvaluedata.ordinal = 0             }             if var subcategoryid = teamdepartmentvaluedict.valueforkey("sub_category_id") as? string             {                 teamdepartmentvaluedata.subcategoryid = subcategoryid.toint()!             }             else             {                 teamdepartmentvaluedata.subcategoryid = 0             }             if var departmentvalueid = teamdepartmentvaluedict.valueforkey("team_department_value_id") as? string             {                 teamdepartmentvaluedata.teamdepartmentvalueid = departmentvalueid.toint()!             }             else{                 teamdepartmentvaluedata.teamdepartmentvalueid = 0             }              sqlite3_bind_int(statement, cint(1), cint(teamdepartmentvaluedata.teamid))             sqlite3_bind_int(statement, cint(2), cint(teamdepartmentvaluedata.departmentid))             sqlite3_bind_int(statement, cint(3), cint(teamdepartmentvaluedata.valueid))             sqlite3_bind_int(statement, cint(4), cint(teamdepartmentvaluedata.ordinal))             sqlite3_bind_int(statement, cint(5), cint(teamdepartmentvaluedata.subcategoryid))             sqlite3_bind_int(statement, cint(6), cint(teamdepartmentvaluedata.teamdepartmentvalueid))              result = sqlite3_step(statement)              if(result != sqlite_done)             {                 println("failed insert")             }             else             {                 println("inserted")             }             sqlite3_clear_bindings(statement);             sqlite3_reset(statement);         }         sqlite3_exec(database, "commit transaction", nil, nil, &errmsg)         sqlite3_exec(database, "end transaction",  nil, nil, &errmsg)         sqlite3_finalize(statement)         sqlite3_close(statement)     } } 

please me solve issue. in advance.

this may not exact answer problem.

as have around 20000 records insert take quite long time. 1 thing comes mind reduce time using dispatch_apply method gcd. check out performing loop iterations concurrently section of this link.

with able execute many loops concurrently on concurrent queue. make execution time shorter. not sure how fast make, give shot.

also make sure not execute each of loop on separate task block club them in considerable chunk avoid unnecessary overhead of scheduling these tasks.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -