realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.67k stars 561 forks source link

Add support for react-native-macos #2907

Open ospfranco opened 4 years ago

ospfranco commented 4 years ago

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?

juhasuni commented 4 years ago

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 commented 4 years ago

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.

ianpward commented 4 years ago

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

kneth commented 4 years ago

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

IbrahimSulai commented 4 years ago

@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.

RichardLindhout commented 4 years ago

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)

kneth commented 3 years ago

@RichardLindhout Did it work for you?

RichardLindhout commented 3 years ago

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

steffenagger commented 3 years ago

Sorry about that...