Skip to content

Commit

Permalink
Merge pull request #3820 from vishalduggal/timob-12133
Browse files Browse the repository at this point in the history
[TIMOB-12133] Reload addressbook when modified externally
  • Loading branch information
srahim committed Feb 4, 2013
2 parents 9b8d10e + f334e15 commit 3ea0793
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
12 changes: 12 additions & 0 deletions apidoc/Titanium/Contacts/Contacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ extends: Titanium.Module
since: "0.8"
platforms: [android, iphone, ipad]

events:
- name: reload
summary: Fired when the database backing the contacts module is modified
description: |
This event signifies that the addressbook reference will be recreated. All existing
[Person](Titanium.Contacts.Person) and [Group](Titanium.Contacts.Group) instances will
still exist but changes to them that modify the addressbook will result in unpredictable
behavior.
platforms: [iphone, ipad]
since: "3.1.0"


methods:
- name: createGroup
summary: Creates and returns an instance of <Titanium.Contacts.Group>.
Expand Down
2 changes: 1 addition & 1 deletion iphone/Classes/ContactsModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@private
ABAddressBookRef addressBook;
ABPeoplePickerNavigationController* picker;

BOOL reloadAddressBook;
BOOL iOS6API;
BOOL animated;
KrollCallback* cancelCallback;
Expand Down
21 changes: 20 additions & 1 deletion iphone/Classes/ContactsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@

@implementation ContactsModule

void CMExternalChangeCallback (ABAddressBookRef notifyAddressBook,CFDictionaryRef info,void *context)
{
DebugLog(@"Got External Change Callback");
ContactsModule* theModule = (ContactsModule*) context;
theModule->reloadAddressBook = YES;
[theModule fireEvent:@"reload" withObject:nil];
}

// We'll force the address book to only be accessed on the main thread, for consistency. Otherwise
// we could run into cross-thread memory issues.
-(ABAddressBookRef)addressBook
Expand All @@ -33,6 +41,12 @@ -(ABAddressBookRef)addressBook
return NULL;
}

if (reloadAddressBook && (addressBook != NULL) ) {
[self releaseAddressBook];
addressBook = NULL;
}
reloadAddressBook = NO;

if (addressBook == NULL) {
if (iOS6API) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_6_0
Expand All @@ -43,14 +57,19 @@ -(ABAddressBookRef)addressBook
}
if (addressBook == NULL) {
DebugLog(@"[WARN] Could not create an address book. Make sure you have gotten permission first.");
} else {
ABAddressBookRegisterExternalChangeCallback(addressBook, CMExternalChangeCallback, self);
}
}
return addressBook;
}

-(void)releaseAddressBook
{
TiThreadPerformOnMainThread(^{CFRelease(addressBook);}, YES);
TiThreadPerformOnMainThread(^{
ABAddressBookUnregisterExternalChangeCallback(addressBook, CMExternalChangeCallback, self);
CFRelease(addressBook);
}, YES);
}

-(void)startup
Expand Down

0 comments on commit 3ea0793

Please sign in to comment.