I am not sure how many people are familiar with the great todo sample app however due to the minimalistic documentation approach it serves as a bible to me right now understanding the joint work of Mobile and Sync Gateway.
However I would have a question about the flow and whether I just don't get it or there is a logic issue within the app.
When looking at the sample I noticed that a profile document gets created as part of the loginWithFacebook function. That works well when the user truly doesn’t exist in the database but what happens when the user logs back in and already has a profile on the remote database? For example I use the sample app on my iPhone, sign up (profile document gets created and synced) -> I also install the app on a a different device and log back in. It is my current understanding that the profile documents gets created again, is that correct? But it doesn’t seem right or how are the two documents with the same document name handled? Do they update incrementally?
Asked a bit differently, our solution requires a profile document however when the user logs in from a device, we want to check whether a profile document already exists, sync first and then use it or create one if it doesn’t exist already?
Can anyone shed some light on that? Am I missing something?
- (void)loginWithFacebookUserInfo:(NSDictionary *)info accessTokenData:(FBAccessTokenData *)tokenData { NSAssert(tokenData, @"Facebook Access Token Data is nil"); NSString *userId = [info objectForKey:@"email"]; NSString *name = [info objectForKey:@"name"]; [self setCurrentUserId:userId]; CBLDatabase *database = [self databaseForUser:userId]; [self setCurrentDatabase:database]; [self setGuestLoggedIn:NO]; Profile *profile = [Profile profileInDatabase:database forUserID:userId]; if (!profile) { profile = [[Profile alloc] initProfileInDatabase:self.database withName:name andUserID:userId]; NSError *error; if ([profile save:&error]) { [self migrateGuestDataToUser:profile]; } else { NSLog(@"Cannot create a new user profile : %@", [error description]); [self showMessage:@"Cannot create a new user profile" withTitle:@"Error"]; } } if (profile) { [self startReplicationWithFacebookAccessToken:tokenData.accessToken]; } }
When looking at the sample I noticed that a profile document gets created as part of the loginWithFacebook function. That works well when the user truly doesn’t exist in the database but what happens when the user logs back in and already has a profile on the remote database?

Asked a bit differently, our solution requires a profile document however when the user logs in from a device, we want to check whether a profile document already exists, sync first and then use it or create one if it doesn’t exist already?
On Oct 23, 2014, at 2:12 PM, Christoph Berlin <appm...@gmail.com> wrote:My question would be whether it is save to assume that I could technically create two documents with the same ID on two different offline devices and Sync Gateway will try to clean up the mess?
If so how do I protect my data logic from running into that problem? Meaning there is a reason that I usually cannot create the same document ID twice but in this scenario it bypasses that check.
If I understand you correctly both documents get created but the version with the higher revision wins, correct?
However lets assume a slightly different scenario:1) iPad creates user_profile document offline at login and then syncs to database2) iPhone is online and synced. When I try to create a NEW document with the ID (same step as creating a user profile offline) I get an error that the document ID already exists, correct?
My question would be whether it is save to assume that I could technically create two documents with the same ID on two different offline devices and Sync Gateway will try to clean up the mess?
If so how do I protect my data logic from running into that problem? Meaning there is a reason that I usually cannot create the same document ID twice but in this scenario it bypasses that check.