From 28e21041bca6268330680b373fbd674bd960ce7a Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 22 Oct 2015 18:06:11 -0700 Subject: [PATCH] bugfix and move gcd out of rpc --- ReactNative/RealmReact.mm | 11 ++++++++--- src/RealmRPC.mm | 31 +++++++++++++------------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ReactNative/RealmReact.mm b/ReactNative/RealmReact.mm index 2a50d1ce..84e24119 100644 --- a/ReactNative/RealmReact.mm +++ b/ReactNative/RealmReact.mm @@ -106,9 +106,14 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool cre processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) { GCDWebServerResponse *response; try { - realm_js::json args = realm_js::json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]); - std::string response_text = rpcServer->perform_request(request.path.UTF8String, args).dump(); - response = [[GCDWebServerDataResponse alloc] initWithData:[NSData dataWithBytes:response_text.c_str() length:response_text.length()] contentType:@"application/json"]; + // perform all realm ops on the main thread + __block NSData *responseData; + dispatch_sync(dispatch_get_main_queue(), ^{ + realm_js::json args = realm_js::json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]); + std::string responseText = rpcServer->perform_request(request.path.UTF8String, args).dump(); + responseData = [NSData dataWithBytes:responseText.c_str() length:responseText.length()]; + }); + response = [[GCDWebServerDataResponse alloc] initWithData:responseData contentType:@"application/json"]; } catch(std::exception &ex) { NSLog(@"Invalid RPC request - %@", [(GCDWebServerDataRequest *)request text]); diff --git a/src/RealmRPC.mm b/src/RealmRPC.mm index 3eabb593..a44a8a55 100644 --- a/src/RealmRPC.mm +++ b/src/RealmRPC.mm @@ -146,8 +146,9 @@ RPCServer::RPCServer() { JSObjectSetPropertyAtIndex(m_context, m_objects[oid], name.get(), value, &exception); } else { - static JSStringRef prop_string = RJSStringForString(name.get()); + JSStringRef prop_string = RJSStringForString(name.get()); JSObjectSetProperty(m_context, m_objects[oid], prop_string, value, 0, &exception); + JSStringRelease(prop_string); } if (exception) { @@ -186,25 +187,19 @@ RPCServer::~RPCServer() { } json RPCServer::perform_request(std::string name, json &args) { - // perform all realm ops on the main thread - __block json response; - dispatch_sync(dispatch_get_main_queue(), ^{ - try { - RPCRequest action = m_requests[name]; - assert(action); + try { + RPCRequest action = m_requests[name]; + assert(action); - if (name == "/create_session" || m_session_id == args["sessionId"].get()) { - response = action(args); - assert(response.is_object()); - } - else { - response = {{"error", "Invalid session ID"}}; - } - } catch (std::exception &exception) { - response = {{"error", (std::string)"exception thrown: " + exception.what()}}; + if (name == "/create_session" || m_session_id == args["sessionId"].get()) { + return action(args); } - }); - return response; + else { + return {{"error", "Invalid session ID"}}; + } + } catch (std::exception &exception) { + return {{"error", (std::string)"exception thrown: " + exception.what()}}; + } } RPCObjectID RPCServer::store_object(JSObjectRef object) {