mercredi 25 novembre 2015

inserting into sqlite database taking too much time - iOS

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 code (This code is running in dispatch_async)

     for record:ABRecordRef in contactList
    {


        contactNumber = ""
        email = ""
        fullName = ""

        if (ABRecordCopyValue(record,
            kABPersonPhoneProperty) != nil)

        {


            if (ABRecordCopyValue(record,
                kABPersonFirstNameProperty) != nil)

            {


                firstName = (ABRecordCopyValue(record, kABPersonFirstNameProperty)?.takeRetainedValue() as? String)!


                if (ABRecordCopyValue(record,
                kABPersonPhoneProperty) != nil)
                {

                       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

                let dateFormatter: NSDateFormatter = NSDateFormatter()
                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

                modificationDate = dateFormatter.stringFromDate(modificationNSDate)
                print("modification date string =\(modificationNSDate)")

                recordId = ABRecordGetRecordID(record)

                print("person id =\(recordId)")

                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 = "";
        print("fullName of person=\(fullName)")
        print("email of person=\(email)")
        print("contact number=\(contactNumber)")

db.insertIntoContact(contactName: fullName, contactNumber: contactNumber, contactEmail: email, recordid : recordId, modifieddate: modificationDate)


    }

Here is insertIntoContact func.

  func insertIntoContact(contactName contactName : String!, contactNumber : String!, contactEmail : String!, recordid:Int32!, modifieddate:String! ) -> Bool
   {

    //sqlite3_bind_int(insertPerson, 1, CInt(pid))

    sqlite3_exec(insertPerson, "BEGIN TRANSACTION", nil, nil, nil)

    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)

     sqlite3_exec(insertPerson, "END TRANSACTION", nil, nil, nil)



    return executeUpdate(sqlStatement: insertPerson)
}

Aucun commentaire:

Enregistrer un commentaire