0

I writting a application, sync contact from server to client. But i have a problem, i get all contact from server to client, When I save a few contacts, application error.

AB: Could not compile statement for query (ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):
SELECT ROWID, Name, ExternalIdentifier, Type, ConstraintsPath, ExternalModificationTag, ExternalSyncTag, AccountID, Enabled, SyncData, MeIdentifier, Capabilities FROM ABStore WHERE Enabled = ?;

Log

2015-02-05 09:54:26.667 SoftFlowContactSync[5367:475593] Authorized : Cầu Khác
2015-02-05 09:54:27.056 SoftFlowContactSync[5367:475593] Authorized : Hoài
2015-02-05 09:54:27.234 SoftFlowContactSync[5367:475593] Authorized : Tùng
2015-02-05 09:54:27.390 SoftFlowContactSync[5367:475593] Authorized : Hà Duy
2015-02-05 09:54:27.478 SoftFlowContactSync[5367:475593] Authorized : Hữu Ru
....
2015-02-05 09:54:27.617 SoftFlowContactSync[5367:475593] Authorized : Duy Cường
AB: Could not compile statement for query (ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):
SELECT ROWID, Name, ExternalIdentifier, Type, ConstraintsPath, ExternalModificationTag, ExternalSyncTag, AccountID, Enabled, SyncData, MeIdentifier, Capabilities FROM ABStore WHERE Enabled = ?;
SQL error
SQL error
2015-02-05 09:54:37.794 SoftFlowContactSync[5367:475593] Authorized : Dũng
AB: Could not compile statement for query (ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):
SELECT ROWID, Name, ExternalIdentifier, Type, ConstraintsPath, ExternalModificationTag, ExternalSyncTag, AccountID, Enabled, SyncData, MeIdentifier, Capabilities FROM ABStore WHERE Enabled = ?;

My code:

Sync

-(void)runSyn
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
    self.txtStatus.text = @"Đang đồng bộ...";
sqlite3_stmt *statement = nil;
const char * sqlFind;
sqlFind = "SELECT first, last, phone, email FROM contact";
const char *dbPath = [_dataBasePath UTF8String];
if(sqlite3_open(dbPath,&_DB)==SQLITE_OK)
{
    sqlite3_prepare_v2(_DB, sqlFind, -1, &statement, NULL);
    while (sqlite3_step(statement) == SQLITE_ROW) {
        NSString* txtFirst = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
        NSString* txtLast = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
        NSString* txtPhone = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
        NSString* txtEmail = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
        [self addContact:txtFirst :txtLast :txtPhone :txtEmail];

    }

    sqlite3_finalize(statement);
}else
{
    [self showUIAlertWithMessage:@"Thất bại trong việc mở dữ liệu!" andTitle:@"Thông báo"];
    NSLog(@"LoadData: %@", @"Thất bại trong việc mở dữ liệu!");

}
sqlite3_close(_DB);
    // update UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^(void){
        self.txtStatus.text = myDateString;
        NSLog(@"RUNSYNC: %@",@"Da add tat ca contact vao thiet bi.");
    });
});

[[NSURLCache sharedURLCache] removeAllCachedResponses];
aTime = nil;
}

AddContact

//Thêm contact
-(void) addContact: (NSString*) txtFirst : (NSString*) txtLast : (NSString*) txtNumber : (NSString *)txtEmail
{

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
    ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted){
    //1
    NSLog(@"Denied");
    UIAlertView *cantAddContactAlert = [[UIAlertView alloc] initWithTitle: @"Không thể thêm được danh bạ!" message: @"Bạn cần cho phép ứng dụng sử dụng danh bạ của máy." delegate:nil cancelButtonTitle: @"Đồng ý" otherButtonTitles: nil];
    [cantAddContactAlert show];
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
    //2
    NSLog(@"Authorized : %@",txtFirst);
    [self Contact:txtFirst:txtLast:txtNumber:txtEmail];
} else
{
    //3
    NSLog(@"Not determined");
    ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (!granted){
                //4
                UIAlertView *cantAddContactAlert = [[UIAlertView alloc] initWithTitle: @"Không thể thêm được danh bạ!" message: @"Bạn cần cho phép ứng dụng sử dụng dạnh bạ của máy." delegate:nil cancelButtonTitle: @"Đồng ý" otherButtonTitles: nil];
                [cantAddContactAlert show];
                return;
            }
            //5
            NSLog(@"Authorized : %@",txtFirst);
            [self Contact:txtFirst:txtLast:txtNumber:txtEmail];
        });
    });
}
}

//Contact
-(void) Contact: (NSString*) txtFirst : (NSString*) txtLast : (NSString*) txtNumber : (NSString*) txtEmail
{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil);
ABRecordRef pet = ABPersonCreate();
ABRecordSetValue(pet, kABPersonFirstNameProperty, (__bridge CFStringRef)txtFirst, nil);
ABRecordSetValue(pet, kABPersonLastNameProperty, (__bridge CFStringRef)txtLast, nil);
ABMutableMultiValueRef phoneNumbers = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumbers, (__bridge CFStringRef)txtNumber, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(pet, kABPersonPhoneProperty, phoneNumbers, nil);
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail, (__bridge CFStringRef)txtEmail, kABWorkLabel, NULL);
ABRecordSetValue(pet, kABPersonEmailProperty, multiEmail, nil);
CFRelease(multiEmail);
ABAddressBookAddRecord(addressBookRef, pet, nil);
NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);
for (id record in allContacts){
    ABRecordRef thisContact = (__bridge ABRecordRef)record;
    if (CFStringCompare(ABRecordCopyCompositeName(thisContact),
                        ABRecordCopyCompositeName(pet), 0) == kCFCompareEqualTo){
        return;
    }
}

ABAddressBookSave(addressBookRef, nil);
}
Takeshi
  • 221
  • 1
  • 4
  • 16

2 Answers2

1

It seems you get some memory issues. Put your code in autorelease block.

Enjoy..coding

 autoreleasepool({})
0

Got the same error today, turned out that I did not have access to the address book.

Rool Paap
  • 1,918
  • 4
  • 32
  • 39