Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug on to-many relationships with Core Data context #1061

Closed
cflorion opened this issue Jan 25, 2016 · 8 comments
Closed

Bug on to-many relationships with Core Data context #1061

cflorion opened this issue Jan 25, 2016 · 8 comments
Assignees
Labels
Milestone

Comments

@cflorion
Copy link
Contributor

Since we started working with Couchbase-lite-ios + CBLIncrementalStore, we've had the same problem: To-many relationships are sometimes not up-to-date in the core data contexts. We decided to add a sample app in Couchbase-lite-ios project in order to isolate this problem.

You can test demo app with #1060

You tap quickly several times on the Add button (we do batches of 10 taps), and after a few tries, the count of players on the right won't be in sync on a team.

@mastohhh
Copy link
Contributor

This is related to #903

@pasin
Copy link
Contributor

pasin commented Jan 25, 2016

Thanks for the test app. I will checkout the app and look at the issue ASAP.

@mastohhh
Copy link
Contributor

We tried to investigate today and we saw that the problem comes from the context stack.

All documents are good. On the server too. We think the problem appears when the pull replication gets 2 batches of documents to close each other.

One with a team and some players and the other with the rest of the players. And sometimes maybe, the rest of the players could be integrated before the team. That could cause this issue.

Is it possible for 2 pull replications to be executed simultaneously? Or are they integrated in a queue?

@snej
Copy link
Contributor

snej commented Jan 26, 2016

There are no ordering guarantees in a replication. The documents pulled from the server could be inserted into the local database in any order. In general the puller is processing docs in the order in which they were last changed on the server, but there are multiple sockets downloading documents in parallel, so you can't predict the order in which they'll be received.

@mastohhh
Copy link
Contributor

The problem comes from the _relationshipCache used in this method :

- (NSArray*) queryToManyRelation: (NSRelationshipDescription*)relation
                    forParentKey: (NSString*)parentKey
                        prefetch: (BOOL)prefetch
                        outError: (NSError**)outError;

It appears when the insertions are in 2 separate batches. For example, we create a new team with 3 players on a device. The other device gets one batch with the team and one player, and the second batch with the 2 new players. Then, the team.players relationship only contains the first player.

This part of the code is the problem :

                SInt64 oldLastSequenceIndexed = view.lastSequenceIndexed;
                [view updateIndex];
                if (view.lastSequenceIndexed == oldLastSequenceIndexed)
                    return [result allObjects];

Maybe the updateIndex has already be done before and now this always returns YES

I don't know what are these indexes and what updateIndexdoes ?

@pasin
Copy link
Contributor

pasin commented Jan 28, 2016

That is the code for optimizing the performance. @mastohhh Does it help the issue if you take that piece of code out?

@zgramana zgramana added this to the 1.3 milestone Jan 29, 2016
@zgramana zgramana added the bug label Jan 29, 2016
@mastohhh
Copy link
Contributor

Yes, when we comment that piece of code, this works again.

@zgramana zgramana modified the milestones: 1.2, 1.3 Jan 29, 2016
@zgramana zgramana self-assigned this Jan 29, 2016
pasin added a commit that referenced this issue Jan 30, 2016
The reverted code is the code that tries to optmize the query by checking if the index is actually updated or not. It looks like that code doesn't work well in some certain circumstances that need more investigation.

#1061
@pasin
Copy link
Contributor

pasin commented Jan 30, 2016

I reverted that code in 1.2.0 release branch. It reverted code doesn't provide much performance optimization anyway. It will get merged to the master branch once we release 1.2.0 branch which should be soon. Thanks for helping checking the issue.

@pasin pasin closed this as completed Jan 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants