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

2.0: Database Listener callbacks not always getting called on main thread #1836

Closed
rajagp opened this Issue Jul 24, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@rajagp
Contributor

rajagp commented Jul 24, 2017

DB 12.0 (Have not checked on Android)
Did the following :

  1.   I registered a DB listener callback on main thread
    
database = try Database(name: username)
        database.addChangeListener { (change) in
            print("Database created on main thread  listener: main: \(Thread.main), current :::: \(Thread.current)")
        }
  1.   Then I did the following on BG thread
    
DispatchQueue.global(qos: .background).async {[weak self] in
           self?.batchTestInBackground()
       }


func batchTestInBackground() {
       print("\(#function):::\(Thread.main):::\(Thread.current)")
       
       let db = try! Database(name: username)
       db.addChangeListener { (change) in
           print("DB Instance created on BG Thread Listener. Main:\(Thread.main), ::::Current:  \(Thread.current)")
       }
           
       try! db.inBatch({
           var i = 0
           while i < 10 {
               let docId = username + ".\(i)." + NSUUID().uuidString
               print(docId)
               let doc = Document(docId)
               doc.set("task-list", forKey: "type")
               doc.set(username, forKey: "name")
               doc.set(username, forKey: "owner")
               do {
                   try db.save(doc)
               } catch let error as NSError {
               }
               i = i + 1
           }
       })
   }

What I observed :
When the DB changes were made on BG thread, the observer callback that was registered on the DB instance (created in BG) was called on the BG thread and the observer callback that was registered on the DB instance (created on main) was called on main thread

Here are the console logs corresponding to the print statements above : -

batchTestInBackground():::<NSThread: 0x600000079a00>{number = 1, name = (null)}:::<NSThread: 0x608000271180>{number = 4, name = (null)}
user1.0.2F469DBB-1753-4AFA-9FC6-2154DC0E723D
user1.1.ED41469D-F15D-48FF-B267-E6B131717D5D
user1.2.1DBF5530-4014-410D-BFB9-5E71BBA8DB15
user1.3.8E09BF5E-858E-4747-99BC-A0E3C262F728
user1.4.258C8F27-4AE4-44A2-B3A4-D50F245DA1CE
user1.5.A41979EC-32B1-46AD-A690-CAC5F6CF91D4
user1.6.896B25A1-7F46-4B0E-B98D-39C60655535D
user1.7.AA8D3FE0-7F93-4245-80B6-585948EB75A8
user1.8.1AEAC83E-B2CC-45EF-9658-EEF2CC88AC40
user1.9.53A89988-E0F9-4747-B130-81AE540893FD
DB Instance created on BG Thread Listener. Main:<NSThread: 0x600000079a00>{number = 1, name = (null)}, ::::Current:  <NSThread: 0x608000271180>{number = 4, name = (null)}
2017-07-24 15:45:39.311 Todo[15769:2676046] [Todo] Replicator: 0/0, error: 
Database created on main thread  listener: main: <NSThread: 0x600000079a00>{number = 1, name = main}, current :::: <NSThread: 0x600000079a00>{number = 1, name = main}

@rajagp rajagp changed the title from 2.0: Swift . Database Listener callbacks not getting called on main thread to 2.0: Swift . Database Listener callbacks not always getting called on main thread Jul 24, 2017

@rajagp

This comment has been minimized.

Contributor

rajagp commented Jul 24, 2017

@hideki : FYI (Can you check this on Android please)

@pasin pasin changed the title from 2.0: Swift . Database Listener callbacks not always getting called on main thread to 2.0: Database Listener callbacks not always getting called on main thread Jul 24, 2017

@pasin

This comment has been minimized.

Contributor

pasin commented Jul 24, 2017

This happens only when the inBatch is called. The inBatch method is posting change directly on the current thread.

https://github.com/couchbase/couchbase-lite-ios/blob/feature/2.0/Objective-C/CBLDatabase.mm#L264

@pasin

This comment has been minimized.

Contributor

pasin commented Jul 24, 2017

I will fix this issue in DB14.

@pasin pasin added this to the 2.0 DB 14 milestone Jul 24, 2017

@pasin pasin self-assigned this Jul 24, 2017

@pasin pasin added the bug label Jul 24, 2017

jamiltz added a commit to couchbaselabs/couchbase-mobile-portal that referenced this issue Jul 25, 2017

jamiltz added a commit to couchbaselabs/couchbase-mobile-portal that referenced this issue Jul 25, 2017

@djpongh djpongh added the ready label Jul 28, 2017

jamiltz added a commit to couchbaselabs/couchbase-mobile-portal that referenced this issue Aug 7, 2017

jamiltz added a commit to couchbaselabs/couchbase-mobile-portal that referenced this issue Aug 7, 2017

@pasin pasin modified the milestones: 2.0 DB 15, 2.0 DB 14 Aug 7, 2017

@pasin pasin modified the milestones: 2.0 DB 16, 2.0 DB 15 Aug 25, 2017

@pasin pasin added backlog and removed ready labels Sep 22, 2017

jamiltz added a commit to couchbaselabs/couchbase-mobile-portal that referenced this issue Oct 4, 2017

jamiltz added a commit to couchbaselabs/couchbase-mobile-portal that referenced this issue Oct 4, 2017

jamiltz added a commit to couchbaselabs/couchbase-mobile-portal that referenced this issue Oct 18, 2017

@djpongh djpongh modified the milestones: 2.0 DB 16, 2.0.0 Nov 17, 2017

@djpongh djpongh added ready and removed backlog labels Nov 17, 2017

jamiltz added a commit to couchbaselabs/couchbase-mobile-portal that referenced this issue Dec 6, 2017

@pasin

This comment has been minimized.

Contributor

pasin commented Dec 15, 2017

This is fixed in 36e4b98.

@pasin pasin closed this Dec 15, 2017

@pasin pasin removed the ready label Dec 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment