realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Add support for react-native-macos

Open ospfranco opened this issue 4 years ago • 9 comments

Goals

Hey! I'm trying to build a new app with react-native, more specifically I'm targeting all platforms, so I'm using react-native-macos, I know it is very new, but just creating a ticket to see if it is possible to bring support to the macOS platform, since it runs swift, I'm guessing it should be fairly easy to add support?

ospfranco avatar Jun 01 '20 07:06 ospfranco

It turned out this was easier than expected. You'll have to modify the RealmJS.podspec to add support for osx platform and also RealmReact.mm to remove code, that won't compile for MacOS.

Here's the complete patch file for the changes I've made:

---
 RealmJS.podspec                           |  3 ++-
 react-native/ios/RealmReact/RealmReact.mm | 23 +----------------------
 2 files changed, 3 insertions(+), 23 deletions(-)

diff --git a/RealmJS.podspec b/RealmJS.podspec
index 7323dbf7..09aaa638 100644
--- a/RealmJS.podspec
+++ b/RealmJS.podspec
@@ -27,7 +27,7 @@ Pod::Spec.new do |s|
 
   s.authors                = package['author']
   s.homepage               = package['homepage']
-  s.platform               = :ios, '9.0'
+  s.platforms    = { :ios => "9.0", :osx => "10.14" }
 
   # The source field is a required field in the podspec, but it is not ment to be used.
   # This is because the Podspec is not ment to be published into a CocoaPod repository, instead React Native uses a :path style dependency when adding this to the users projects Podfile.
@@ -90,6 +90,7 @@ Pod::Spec.new do |s|
   
   # TODO: Consider providing an option to build with the -dbg binaries instead
   s.ios.vendored_libraries = 'vendor/realm-ios/librealm-ios.a', 'vendor/realm-ios/librealm-parser-ios.a'
+  s.osx.vendored_libraries = 'vendor/realm-ios/librealm-macosx.a', 'vendor/realm-ios/librealm-parser-macosx.a'
   # s.watchos.vendored_libraries = 'vendor/realm-ios/librealm-watchos.a', 'vendor/realm-ios/librealm-parser-watchos.a'
   # s.tvos.vendored_libraries = 'vendor/realm-ios/librealm-tvos.a', 'vendor/realm-ios/librealm-parser-tvos.a'
 
diff --git a/react-native/ios/RealmReact/RealmReact.mm b/react-native/ios/RealmReact/RealmReact.mm
index 9c1ce061..ede9a0dc 100644
--- a/react-native/ios/RealmReact/RealmReact.mm
+++ b/react-native/ios/RealmReact/RealmReact.mm
@@ -58,28 +58,7 @@ - (void *)runtime;
 @end
 
 extern "C" JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool create) {
-    Ivar contextIvar = class_getInstanceVariable([executor class], "_context");
-    if (!contextIvar) {
-        return NULL;
-    }
-
-    id rctJSContext = object_getIvar(executor, contextIvar);
-    if (!rctJSContext && create) {
-        Class RCTJavaScriptContext = NSClassFromString(@"RCTJavaScriptContext");
-        if ([RCTJavaScriptContext instancesRespondToSelector:@selector(initWithJSContext:onThread:)]) {
-            // for RN 0.28.0+
-            rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:[JSContext new] onThread:[NSThread currentThread]];
-        }
-        else {
-            // for RN < 0.28.0
-            assert([RCTJavaScriptContext instancesRespondToSelector:@selector(initWithJSContext:)]);
-            rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:[JSContext new]];
-        }
-
-        object_setIvar(executor, contextIvar, rctJSContext);
-    }
-
-    return [rctJSContext context].JSGlobalContextRef;
+    return NULL;
 }
 
 @interface RealmReact () <RCTBridgeModule>
-- 

RealmReactGetJSGlobalContextForExecutor function is needed only if running React Native <= 0.44.

Now, I'd like to get some feedback from the developers of RealmJS, why this is not officially supported and if there's something that won't work. I've only done a very basic test to see that Realm can be opened and models can be inserted into the database.

juhasuni avatar Jun 09 '20 05:06 juhasuni

Note: react-native-macos requires Node 12, which is not supported by Realm. So make sure to first install Realm (using npm or yarn) with Realm-compatible Node version (I used 10.x) and then install MacOS support using Node 12.

juhasuni avatar Jun 09 '20 10:06 juhasuni

Realm supports node12 https://github.com/realm/realm-js/issues/2516

ianpward avatar Jun 09 '20 15:06 ianpward

@juhasuni Can I ask you to create a proper pull request?

kneth avatar Jun 12 '20 08:06 kneth

@juhasuni - It would be really great if you create PR for the support react-native-macos?

As explained above, I have tried to change the RealmJS.podspec and RealmReact.mm manually. But, it throwing build errors.

Thanks in advance for the effort.

IbrahimSulai avatar Jun 17 '20 14:06 IbrahimSulai

Worked for me with @juhasuni and node v14.3.0

Got an error for the js version string in a/node_modules/realm/react-native/ios/RealmReact/RealmAnalytics.mm

And commented that, now it builds and starts (have not tested creating etc yet)

RichardLindhout avatar Jun 27 '20 14:06 RichardLindhout

@RichardLindhout Did it work for you?

kneth avatar Jul 13 '20 10:07 kneth

Yes, it works with getting/creating/inserting etc no issues so far

RichardLindhout avatar Jul 13 '20 12:07 RichardLindhout

Sorry about that...

steffenagger avatar Jul 27 '20 14:07 steffenagger