I have 6000 records of PhoneBook I'm inserting into sqlite its taking 45 seconds, that is huge time.
for each record I want only few properties like name, email, id, modified date. So atleast one for loop i need because of that its taking 45 seconds. How can i reduce?
Here is the updated code (This code is running in dispatch_async)
I also checked similar problem How to insert 40000 records fast into an sqlite database in an iPad solution says I've to use BEGIN & END transaction that i used already but still facing the same.
UPDATE - As per suggested solutions i've updated my code but still its taking 45 seconds. Please help me.
sqlite3_exec(db.insertPerson, "BEGIN TRANSACTION", nil, nil, nil)
for record:ABRecordRef in contactList
{
contactNumber = ""
email = ""
fullName = ""
if (ABRecordCopyValue(record,kABPersonPhoneProperty) != nil) && (ABRecordCopyValue(record,kABPersonFirstNameProperty) != nil)
{
firstName = (ABRecordCopyValue(record, kABPersonFirstNameProperty)?.takeRetainedValue() as? String)!
let numbers:ABMultiValue = ABRecordCopyValue(record, kABPersonPhoneProperty).takeRetainedValue()
if (ABMultiValueGetCount(numbers) > 0)
{
contactNumber = (ABMultiValueCopyValueAtIndex(numbers,0)?.takeRetainedValue() as? String)!
}
let modificationNSDate = (ABRecordCopyValue(record, kABPersonModificationDateProperty)?.takeRetainedValue())! as! NSDate
modificationDate = dateFormatter.stringFromDate(modificationNSDate)
recordId = ABRecordGetRecordID(record)
if (ABRecordCopyValue(record,
kABPersonLastNameProperty) != nil)
{
lastName = (ABRecordCopyValue(record,
kABPersonLastNameProperty).takeRetainedValue()as? String)!
}
let emails: ABMultiValueRef = ABRecordCopyValue(record, kABPersonEmailProperty).takeRetainedValue()
for (var i = 0; i < ABMultiValueGetCount(emails); i++)
{
email = ABMultiValueCopyValueAtIndex(emails, i).takeRetainedValue() as! String
}
}
fullName = "\(firstName) \(lastName)";
lastName = "";
db.insertIntoContact(contactName: fullName, contactNumber: contactNumber, contactEmail: email, recordid : recordId, modifieddate: modificationDate)
}
sqlite3_exec(db.insertPerson, "END TRANSACTION", nil, nil, nil)
Here is insertIntoContact func.
func insertIntoContact(contactName contactName : String!, contactNumber : String!, contactEmail : String!, recordid:Int32!, modifieddate:String! ) -> Bool
{
sqlite3_bind_text(insertPerson, 1, (contactName as NSString).UTF8String, -1, nil)
sqlite3_bind_text(insertPerson, 2, (contactNumber as NSString).UTF8String, -1, nil)
sqlite3_bind_text(insertPerson, 3, (contactEmail as NSString).UTF8String, -1, nil)
sqlite3_bind_int(insertPerson, 4, Int32(recordid))
sqlite3_bind_text(insertPerson, 5, (modifieddate as NSString).UTF8String, -1, nil)
return executeUpdate(sqlStatement: insertPerson)
}
For more details
func executeUpdate(sqlStatement statement:COpaquePointer) -> Bool
{
let resultCode = executeStatement(sqlStatement: statement, success:Int(SQLITE_DONE))
sqlite3_reset(statement)
return resultCode
}
func executeStatement(sqlStatement statement:COpaquePointer,success successConstant:Int) -> Bool
{
let success = Int(sqlite3_step(statement))
if success != successConstant
{
print("Statement \(successConstant) failed with error \(success)")
return false
}
return true
}