{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/4761","repository_url":"https://api.github.com/repos/realm/realm-cocoa","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/4761/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/4761/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/4761/events","html_url":"https://github.com/realm/realm-cocoa/issues/4761","id":216313493,"number":4761,"title":"Issue with updates when changes happen on background thread are started before subscription","user":{"login":"grangej","id":604788,"avatar_url":"https://avatars3.githubusercontent.com/u/604788?v=3","gravatar_id":"","url":"https://api.github.com/users/grangej","html_url":"https://github.com/grangej","followers_url":"https://api.github.com/users/grangej/followers","following_url":"https://api.github.com/users/grangej/following{/other_user}","gists_url":"https://api.github.com/users/grangej/gists{/gist_id}","starred_url":"https://api.github.com/users/grangej/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grangej/subscriptions","organizations_url":"https://api.github.com/users/grangej/orgs","repos_url":"https://api.github.com/users/grangej/repos","events_url":"https://api.github.com/users/grangej/events{/privacy}","received_events_url":"https://api.github.com/users/grangej/received_events","type":"User","site_admin":false},"labels":[{"id":3303146,"url":"https://api.github.com/repos/realm/realm-cocoa/labels/T:Help","name":"T:Help","color":"cc317c","default":false}],"state":"open","locked":false,"assignee":{"login":"icanzilb","id":633862,"avatar_url":"https://avatars3.githubusercontent.com/u/633862?v=3","gravatar_id":"","url":"https://api.github.com/users/icanzilb","html_url":"https://github.com/icanzilb","followers_url":"https://api.github.com/users/icanzilb/followers","following_url":"https://api.github.com/users/icanzilb/following{/other_user}","gists_url":"https://api.github.com/users/icanzilb/gists{/gist_id}","starred_url":"https://api.github.com/users/icanzilb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/icanzilb/subscriptions","organizations_url":"https://api.github.com/users/icanzilb/orgs","repos_url":"https://api.github.com/users/icanzilb/repos","events_url":"https://api.github.com/users/icanzilb/events{/privacy}","received_events_url":"https://api.github.com/users/icanzilb/received_events","type":"User","site_admin":false},"assignees":[{"login":"icanzilb","id":633862,"avatar_url":"https://avatars3.githubusercontent.com/u/633862?v=3","gravatar_id":"","url":"https://api.github.com/users/icanzilb","html_url":"https://github.com/icanzilb","followers_url":"https://api.github.com/users/icanzilb/followers","following_url":"https://api.github.com/users/icanzilb/following{/other_user}","gists_url":"https://api.github.com/users/icanzilb/gists{/gist_id}","starred_url":"https://api.github.com/users/icanzilb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/icanzilb/subscriptions","organizations_url":"https://api.github.com/users/icanzilb/orgs","repos_url":"https://api.github.com/users/icanzilb/repos","events_url":"https://api.github.com/users/icanzilb/events{/privacy}","received_events_url":"https://api.github.com/users/icanzilb/received_events","type":"User","site_admin":false},{"login":"austinzheng","id":3687054,"avatar_url":"https://avatars0.githubusercontent.com/u/3687054?v=3","gravatar_id":"","url":"https://api.github.com/users/austinzheng","html_url":"https://github.com/austinzheng","followers_url":"https://api.github.com/users/austinzheng/followers","following_url":"https://api.github.com/users/austinzheng/following{/other_user}","gists_url":"https://api.github.com/users/austinzheng/gists{/gist_id}","starred_url":"https://api.github.com/users/austinzheng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/austinzheng/subscriptions","organizations_url":"https://api.github.com/users/austinzheng/orgs","repos_url":"https://api.github.com/users/austinzheng/repos","events_url":"https://api.github.com/users/austinzheng/events{/privacy}","received_events_url":"https://api.github.com/users/austinzheng/received_events","type":"User","site_admin":false}],"milestone":null,"comments":6,"created_at":"2017-03-23T06:11:48Z","updated_at":"2017-04-01T10:32:19Z","closed_at":null,"body":"\r\n## Goals\r\n\r\nI am trying to subscribe using RealmSwift + RxRealm to changes to a collection with a simple query, I need to be able to get the current results and also any changes that happen later. \r\n\r\n## Expected Results\r\n\r\nI expect that creating observable query should get the current state of the data base and new changes. \r\n\r\n## Actual Results\r\n\r\nCurrently this works as long as initial changes to objects in database either happen on same thread OR the background updates to changes are started AFTER the subscription has happened.\r\n\r\n## Steps to Reproduce\r\n\r\n- Create a simple query returning the array of objects as an observable.\r\n\r\n- Kick off a background update to an objet in the database\r\n\r\n- subscribe to the changes to that query\r\n\r\n- Observe you only get the state of the object before the background update, the changes made by the background update are never received. \r\n\r\n****  NOTE: If you kick off the background update AFTER the subscription happened this works fine. \r\n\r\n**** NOTE2: In my app it is not possible to know if there is already an update to the database happening before subscriptions happen , so it is not feasible to always subscribe first, subscriptions happen at any time during the course of the app for UI updates. \r\n\r\n## Code Sample\r\n\r\n```swift\r\nfunc testRXRealm() {\r\n        \r\n        let waitForStart = self.expectation(description: \"We should get a start event for our operation\")\r\n\r\n        do {\r\n            \r\n            guard let accountId = self.accountOperation?.account?.id else {\r\n                \r\n                XCTFail(\"No account op\")\r\n                \r\n                return\r\n            }\r\n            \r\n            let realm = try Realm()\r\n            \r\n            let operations = realm.objects(AccountOperation.self).filter(\"account.id = %@\", accountId)\r\n            \r\n            let obsOperations = Observable.array(from: operations).share()\r\n            \r\n            guard let accountOp = self.accountOperation?.detached() else {\r\n                \r\n                XCTFail(\"We should get accountOP\")\r\n                \r\n                return\r\n            }\r\n            \r\n            \r\n            realm.beginWrite()\r\n            \r\n            accountOp.taskState = .Executing\r\n            \r\n            realm.add(accountOp, update: true)\r\n            \r\n            try realm.commitWrite()\r\n            \r\n            \r\n            guard let accountOp2 = self.accountOperation?.detached() else {\r\n                \r\n                XCTFail(\"We should get accountOP\")\r\n                \r\n                return\r\n            }\r\n            \r\n            \r\n            self.queryQueue.async {\r\n                \r\n                let bgRealm = try! Realm()\r\n                \r\n                bgRealm.beginWrite()\r\n                \r\n                accountOp2.taskState = .Finished\r\n                \r\n                bgRealm.add(accountOp2, update: true)\r\n                \r\n                try! bgRealm.commitWrite()\r\n            }\r\n\r\n            \r\n            obsOperations.subscribe(onNext: { (operations) in\r\n                \r\n                XCTAssertGreaterThan(operations.count, 0, \"There should be more than 0 operations in the database\")\r\n                \r\n                for op in operations {\r\n                    \r\n                    switch op.taskState {\r\n                        \r\n                    case .Executing:\r\n                        \r\n                        print(\"Executing\")\r\n                        \r\n                        \r\n                    case .Finished:\r\n                        \r\n                        print(\"Finished\")\r\n                        \r\n                        waitForStart.fulfill()\r\n\r\n                        \r\n                    case .Init:\r\n                        \r\n                        print(\"Init\")\r\n                        \r\n                    default:\r\n                        \r\n                        break\r\n                    }\r\n                }\r\n                \r\n            }, onError: { (error) in\r\n\r\n                print(\"error: \\(error.localizedDescription)\")\r\n                \r\n            }, onCompleted: { \r\n                \r\n                print(\"complete\")\r\n                \r\n            }, onDisposed: nil).addDisposableTo(self.disposeBag)\r\n\r\n            \r\n\r\n            \r\n        }\r\n        catch let error {\r\n            \r\n            error.logError()\r\n            \r\n            XCTFail(\"\\(error.localizedDescription)\")\r\n        }\r\n        \r\n        \r\n        \r\n        self.waitForExpectations(timeout: 5) { (error) in\r\n            \r\n            if let error = error {\r\n                \r\n                error.logError()\r\n                \r\n            }\r\n        }\r\n    }\r\n   \r\n```\r\n\r\n## Version of Realm and Tooling\r\n\r\n```\r\nProductName:\tMac OS X\r\nProductVersion:\t10.12.3\r\nBuildVersion:\t16D32\r\n\r\n/Applications/Xcode.app/Contents/Developer\r\nXcode 8.2.1\r\nBuild version 8C1002\r\n\r\n/usr/local/bin/pod\r\n1.2.0\r\n(not in use here)\r\n\r\n/bin/bash\r\nGNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)\r\n\r\n/usr/local/bin/carthage\r\n0.20.1\r\n(not in use here)\r\n\r\n/usr/bin/git\r\ngit version 2.10.1 (Apple Git-78)\r\n```\r\n\r\nRealm version: \r\n\r\ngithub \"RxSwiftCommunity/RxRealm\" \"0.5.2\"\r\ngithub \"realm/realm-cocoa\" \"v2.4.4\"\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/290911721","html_url":"https://github.com/realm/realm-cocoa/issues/4761#issuecomment-290911721","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/4761","id":290911721,"user":{"login":"icanzilb","id":633862,"avatar_url":"https://avatars3.githubusercontent.com/u/633862?v=3","gravatar_id":"","url":"https://api.github.com/users/icanzilb","html_url":"https://github.com/icanzilb","followers_url":"https://api.github.com/users/icanzilb/followers","following_url":"https://api.github.com/users/icanzilb/following{/other_user}","gists_url":"https://api.github.com/users/icanzilb/gists{/gist_id}","starred_url":"https://api.github.com/users/icanzilb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/icanzilb/subscriptions","organizations_url":"https://api.github.com/users/icanzilb/orgs","repos_url":"https://api.github.com/users/icanzilb/repos","events_url":"https://api.github.com/users/icanzilb/events{/privacy}","received_events_url":"https://api.github.com/users/icanzilb/received_events","type":"User","site_admin":false},"created_at":"2017-04-01T10:32:19Z","updated_at":"2017-04-01T10:32:19Z","body":"If you create the observable asynchronously you will receive the background thread changes like so:\r\n\r\n```swift\r\nDispatchQueue.global(qos: .background).async {\r\n    let realm = try! Realm()\r\n    let obj = realm.objects(Test.self).first!\r\n\r\n    try! realm.write {\r\n        obj.name = \"Test0\"\r\n    }\r\n}\r\n\r\nObservable.array(from: tests, synchronousStart: false)\r\n    .subscribe(onNext: { value in\r\n        print(\"rx next: \\(flatten(value))\")\r\n    })\r\n    .addDisposableTo(bag)\r\n```\r\n\r\nThat still doesn't explain what the problem is in the first place though, will look a bit more into this"}}
{"actor":{"display_login":"icanzilb"}}