{"id":623,"date":"2003-08-23T13:01:50","date_gmt":"2003-08-23T17:01:50","guid":{"rendered":"\/?p=623"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-30T04:00:00","slug":"nsobjectinaccessibleexcep","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2003\/08\/23\/nsobjectinaccessibleexcep\/","title":{"rendered":"NSObjectInaccessibleException"},"content":{"rendered":"<p>\nToday I learned something about <a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Conceptual\/DistrObjects\/\">distributed objects<\/a> and <a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/Foundation\/ObjC_classic\/Classes\/NSConnection.html#\/\/apple_ref\/occ\/cl\/NSConnection\">NSConnection<\/a>. When I have an object that I want to access from multiple threads (or that I want to receive messages in the main thread), I make it a DO server, and I give each thread a proxy. I&rsquo;ve been creating the proxies like this:\n<\/p>\n\n<pre>\nNSConnection *clientConnection = [[[NSConnection alloc]\n                           initWithReceivePort:[serverConnection sendPort]\n                                      sendPort:[serverConnection receivePort]];\nid proxy = [clientConnection rootProxy];\n<\/pre>\n\n<p>\nThere is only one <code>serverConnection<\/code> per server, but that&rsquo;s OK because I call <code>[serverConnection enableMultipleThreads]<\/code>. The problem was that I was inexplicably getting <code>NSObjectInaccessibleException<\/code>s saying &ldquo;NSDistantObject access attempted from another thread&rdquo; when two threads tried to create proxies for the same server.\n<\/p>\n\n<p>\nThe above code makes it look like there&rsquo;s one <code>clientConnection<\/code> per thread\/proxy. However, the documentation for the <code>-initWithReceivePort:sendPort:<\/code> method says:\n<\/p>\n\n<blockquote>\nIf an NSConnection with the same ports already exists, releases the receiver, retains the existing NSConnection, and returns it.\n<\/blockquote>\n\n<p>\nThis means that the connection will be shared among multiple threads and you have to call <code>[clientConnection enableMultipleThreads]<\/code> to avoid the <code>NSObjectInaccessibleException<\/code>. I&rsquo;m not a big fan of this design, because I don&rsquo;t think code using a given connection should have to know when some other code is using a connection with the same ports. But there you have it.\n<\/p>","protected":false},"excerpt":{"rendered":"<p>Today I learned something about distributed objects and NSConnection. When I have an object that I want to access from multiple threads (or that I want to receive messages in the main thread), I make it a DO server, and I give each thread a proxy. I&rsquo;ve been creating the proxies like this: NSConnection *clientConnection [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-623","post","type-post","status-publish","format-standard","hentry","category-programming-category"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=623"}],"version-history":[{"count":0,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/623\/revisions"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}