{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/3136","repository_url":"https://api.github.com/repos/realm/realm-cocoa","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3136/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3136/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3136/events","html_url":"https://github.com/realm/realm-cocoa/issues/3136","id":128627498,"number":3136,"title":"Support for asynchronous writes","user":{"login":"astigsen","id":69422,"avatar_url":"https://avatars.githubusercontent.com/u/69422?v=3","gravatar_id":"","url":"https://api.github.com/users/astigsen","html_url":"https://github.com/astigsen","followers_url":"https://api.github.com/users/astigsen/followers","following_url":"https://api.github.com/users/astigsen/following{/other_user}","gists_url":"https://api.github.com/users/astigsen/gists{/gist_id}","starred_url":"https://api.github.com/users/astigsen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/astigsen/subscriptions","organizations_url":"https://api.github.com/users/astigsen/orgs","repos_url":"https://api.github.com/users/astigsen/repos","events_url":"https://api.github.com/users/astigsen/events{/privacy}","received_events_url":"https://api.github.com/users/astigsen/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/S:In%20Progress","name":"S:In Progress","color":"ededed"},{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/T:Feature","name":"T:Feature","color":"009800"}],"state":"open","locked":false,"assignee":{"login":"JadenGeller","id":4544152,"avatar_url":"https://avatars.githubusercontent.com/u/4544152?v=3","gravatar_id":"","url":"https://api.github.com/users/JadenGeller","html_url":"https://github.com/JadenGeller","followers_url":"https://api.github.com/users/JadenGeller/followers","following_url":"https://api.github.com/users/JadenGeller/following{/other_user}","gists_url":"https://api.github.com/users/JadenGeller/gists{/gist_id}","starred_url":"https://api.github.com/users/JadenGeller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JadenGeller/subscriptions","organizations_url":"https://api.github.com/users/JadenGeller/orgs","repos_url":"https://api.github.com/users/JadenGeller/repos","events_url":"https://api.github.com/users/JadenGeller/events{/privacy}","received_events_url":"https://api.github.com/users/JadenGeller/received_events","type":"User","site_admin":false},"assignees":[{"login":"JadenGeller","id":4544152,"avatar_url":"https://avatars.githubusercontent.com/u/4544152?v=3","gravatar_id":"","url":"https://api.github.com/users/JadenGeller","html_url":"https://github.com/JadenGeller","followers_url":"https://api.github.com/users/JadenGeller/followers","following_url":"https://api.github.com/users/JadenGeller/following{/other_user}","gists_url":"https://api.github.com/users/JadenGeller/gists{/gist_id}","starred_url":"https://api.github.com/users/JadenGeller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JadenGeller/subscriptions","organizations_url":"https://api.github.com/users/JadenGeller/orgs","repos_url":"https://api.github.com/users/JadenGeller/repos","events_url":"https://api.github.com/users/JadenGeller/events{/privacy}","received_events_url":"https://api.github.com/users/JadenGeller/received_events","type":"User","site_admin":false}],"milestone":null,"comments":19,"created_at":"2016-01-25T20:14:31Z","updated_at":"2016-06-28T18:06:27Z","closed_at":null,"body":"What are the current thoughts on adding a shortcut syntax for doing async transactions (where operations happen on a background thread)?\r\n\r\nHere is the current recommended way of doing it manually (from the docs):\r\n\r\n```swift\r\n// Query from any thread\r\ndispatch_async(dispatch_queue_create(\"background\", nil)) {\r\n  let realm = try! Realm()\r\n  let theDog = realm.objects(Dog).filter(\"age == 1\").first\r\n  try! realm.write {\r\n    theDog!.age = 3\r\n  }\r\n}\r\n```\r\n\r\nIt is very flexible, but it does open up the opportunity to accidentally forgetting to get a new Realm, and when working with non-default realms, you have to be careful to pass it the right configuration (also, you probably want it wrapped in an autoreleasepool, like in some of the later examples).\r\n\r\nWould it make sense to offer a simpler version that ensured that you get the right realm (and that it get released afterwards)? Maybe something like this:\r\n\r\n```swift\r\n// Query and update on background thread\r\nrealm.async { realm in\r\n  let theDog = realm.objects(Dog).filter(\"age == 1\").first\r\n  try! realm.write {\r\n    theDog!.age = 3\r\n  }\r\n}\r\n```\r\n\r\nOr even simpler if you know that you need to write:\r\n\r\n```swift\r\n// Write via background thread\r\nrealm.async_write { realm in\r\n  let theDog = realm.objects(Dog).filter(\"age == 1\").first\r\n  theDog!.age = 3\r\n}\r\n```\r\n\r\nThis kind of shortcut syntax would obviously need a bit of thought in regard to how you would then handle errors (can't open realm, commit fails), but offering the option to supply a closure called on error might satisfy that."},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/229132640","html_url":"https://github.com/realm/realm-cocoa/issues/3136#issuecomment-229132640","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3136","id":229132640,"user":{"login":"bigfish24","id":1036685,"avatar_url":"https://avatars.githubusercontent.com/u/1036685?v=3","gravatar_id":"","url":"https://api.github.com/users/bigfish24","html_url":"https://github.com/bigfish24","followers_url":"https://api.github.com/users/bigfish24/followers","following_url":"https://api.github.com/users/bigfish24/following{/other_user}","gists_url":"https://api.github.com/users/bigfish24/gists{/gist_id}","starred_url":"https://api.github.com/users/bigfish24/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bigfish24/subscriptions","organizations_url":"https://api.github.com/users/bigfish24/orgs","repos_url":"https://api.github.com/users/bigfish24/repos","events_url":"https://api.github.com/users/bigfish24/events{/privacy}","received_events_url":"https://api.github.com/users/bigfish24/received_events","type":"User","site_admin":false},"created_at":"2016-06-28T18:06:27Z","updated_at":"2016-06-28T18:06:27Z","body":"Why not follow @tgoyne [suggestion](https://github.com/realm/realm-cocoa/pull/3369#issuecomment-200937115) and just have it accept a single object which is either an object or a collection of objects, thus you only have to cast back into that?"}}
{"actor":{"display_login":"bigfish24"}}