{"action":"created","issue":{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/938","labels_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/938/labels{/name}","comments_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/938/comments","events_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/938/events","html_url":"https://github.com/couchbase/couchbase-lite-ios/issues/938","id":109958414,"number":938,"title":"CBLIS - Very poor performances from 5000 documents locally","user":{"login":"mastohhh","id":1760064,"avatar_url":"https://avatars.githubusercontent.com/u/1760064?v=3","gravatar_id":"","url":"https://api.github.com/users/mastohhh","html_url":"https://github.com/mastohhh","followers_url":"https://api.github.com/users/mastohhh/followers","following_url":"https://api.github.com/users/mastohhh/following{/other_user}","gists_url":"https://api.github.com/users/mastohhh/gists{/gist_id}","starred_url":"https://api.github.com/users/mastohhh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mastohhh/subscriptions","organizations_url":"https://api.github.com/users/mastohhh/orgs","repos_url":"https://api.github.com/users/mastohhh/repos","events_url":"https://api.github.com/users/mastohhh/events{/privacy}","received_events_url":"https://api.github.com/users/mastohhh/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/labels/bug","name":"bug","color":"e11d21"},{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/labels/core-data","name":"core-data","color":"006b75"},{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/labels/performance","name":"performance","color":"02e10c"},{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/labels/ready","name":"ready","color":"ededed"},{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/labels/reproduce/investigate","name":"reproduce/investigate","color":"fbca04"}],"state":"open","locked":false,"assignee":{"login":"pasin","id":801454,"avatar_url":"https://avatars.githubusercontent.com/u/801454?v=3","gravatar_id":"","url":"https://api.github.com/users/pasin","html_url":"https://github.com/pasin","followers_url":"https://api.github.com/users/pasin/followers","following_url":"https://api.github.com/users/pasin/following{/other_user}","gists_url":"https://api.github.com/users/pasin/gists{/gist_id}","starred_url":"https://api.github.com/users/pasin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pasin/subscriptions","organizations_url":"https://api.github.com/users/pasin/orgs","repos_url":"https://api.github.com/users/pasin/repos","events_url":"https://api.github.com/users/pasin/events{/privacy}","received_events_url":"https://api.github.com/users/pasin/received_events","type":"User","site_admin":false},"milestone":null,"comments":6,"created_at":"2015-10-06T08:23:51Z","updated_at":"2015-10-14T20:16:43Z","closed_at":null,"body":"Despite the optimizations @pasin did here : https://github.com/couchbase/couchbase-lite-ios/issues/793, I still encounter performance issues.\r\n\r\nI really don't know what's the best practice to use both `CBLManager` and `NSManagedObjectContext`. Here's what I tried : \r\n\r\n## 1 - Parent Context - Private context's queue : \r\n\r\n- A main context that has a parent context\r\n- A private context parent of the main, and with a coordinator.\r\n- `dispatchQueue` of `CBLManager` is the same as the private context's queue. \r\n\r\nThis is the one I actually use.\r\n\r\nThe problem with this one is `dispatch_get_current_queue();` will be deprecated. And it's not really fast.\r\n\r\n## 2 - Parent Context - Background Queue\r\n\r\n- A main context that has a parent context\r\n- A private context parent of the main, and with a coordinator.\r\n- `dispatchQueue` of `CBLManager` is `dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)`.\r\n\r\nIt gives sometimes crashes like :  `The FMDatabase <CBL_FMDatabase: 0x7ae753c0> is currently in use.`\r\n`*** Assertion failure in -[CBL_FMDatabase warnInUse], FMDatabase.m:360`\r\n\r\n## 3 - Main Context - Main Thread : \r\n\r\n- Only one context in the main thread with the coordinator.\r\n- `dispatchQueue` of `CBLManager` is by default, main thread.\r\n\r\nThis is very slow\r\n\r\n## 4 - Main Context - Background Queue : \r\n\r\n- Only one context in the main thread with the coordinator.\r\n- `dispatchQueue` of `CBLManager` is `dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)`.\r\n\r\nEvery calls to `performBlock:` fails.\r\nSlow and crashes like :  `The FMDatabase <CBL_FMDatabase: 0x7ae753c0> is currently in use.`\r\n\r\n## 5 - Confinment Context - Background Queue\r\n\r\n- `dispatchQueue` of `CBLManager` is `dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)`.\r\n- Only one context `NSConfinementConcurrencyType` (deprecated) in the `dispatchQueue` of `CBLManager`.\r\n\r\nError message `Can only use -performBlock: on an NSManagedObjectContext that was created with a queue.`\r\n\r\n## Conclusion\r\n\r\nPerformance goes down as the global number of documents increase.\r\n\r\nOur app needs to handle thousands of documents (our firsts users maybe 100 000). There was no problem with classic CoreData SQLite. Now we have to purge old documents, and still remains problems of performance. Both in the saves, as in the fetches.\r\n\r\nAs iOS9 is available on iPad 2, we can't have an app that blocks for many seconds during usage. iPad Air 1 has UI lags from 5000 documents. \r\n\r\nI think pure couchbase-lite-ios without CoreData can be fast with `CBLManager`'s `dispatchQueue`, but maybe it's not design for `NSManagedObjectContext`s ?\r\n\r\nDo you have a CoreData stack that works fine ?\r\n\r\nDo you think `CBLIncrementalStore` can be optimized again ? I saw that `CBLQueryEnumerator* rows = [query run: outError];` is called often and behind there's `int rc = sqlite3_step(statement.statement);` and I don't think we can optimize that.\r\n\r\nDoes ForestDB is 100x faster ?\r\n\r\nThanks"},"comment":{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/comments/148182802","html_url":"https://github.com/couchbase/couchbase-lite-ios/issues/938#issuecomment-148182802","issue_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/938","id":148182802,"user":{"login":"mastohhh","id":1760064,"avatar_url":"https://avatars.githubusercontent.com/u/1760064?v=3","gravatar_id":"","url":"https://api.github.com/users/mastohhh","html_url":"https://github.com/mastohhh","followers_url":"https://api.github.com/users/mastohhh/followers","following_url":"https://api.github.com/users/mastohhh/following{/other_user}","gists_url":"https://api.github.com/users/mastohhh/gists{/gist_id}","starred_url":"https://api.github.com/users/mastohhh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mastohhh/subscriptions","organizations_url":"https://api.github.com/users/mastohhh/orgs","repos_url":"https://api.github.com/users/mastohhh/repos","events_url":"https://api.github.com/users/mastohhh/events{/privacy}","received_events_url":"https://api.github.com/users/mastohhh/received_events","type":"User","site_admin":false},"created_at":"2015-10-14T20:16:43Z","updated_at":"2015-10-14T20:16:43Z","body":"More or less a thousand. "}}