realm / realm-js

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

Support React Native installed with CocoaPods #884

Closed afces1 closed 4 years ago

afces1 commented 7 years ago

Goals: adding RealmReact 1.0.2 project into XCode with pod install of react-native@0.42.0.rc-3 will not result in compilation failure.

Expected results: success app build using RealmReact project with react-native@0.42.0.rc-3 inside XCode project.

Actual results: compilation failure on RealmReact.mm: 'React/RCTBridgeDelegate.h' file not found in RCTBridge.h. This is most likely due to the iOS header breaking change introduced in react-native@0.40.0 where React Native headers are expected to be enclosed in angle brackets, e.g. <React/RCTBridge.h>. Previously working on react-native@0.38.0 using same app code.

Steps to reproduce: directory structure: $root/app, $root/rn (app XCode project is sibling of react native directory rn)

  1. npm install react@15.4.2 react-native@0.42.0.rc-3
  2. npm install realm@1.0.2
  3. include Yoga and React in Podfile: target 'app' do

use_frameworks!

pod 'Fabric' pod 'Crashlytics'

  1. React libraries pod 'Yoga', :path => '../rn/node_modules/react-native/ReactCommon/yoga', :inhibit_warnings => true pod 'React', :path => '../rn/node_modules/react-native', :subspecs => [ 'Core', 'RCTActionSheet', 'RCTAnimation', 'RCTImage', 'RCTLinkingIOS', 'RCTText', 'RCTNetwork', 'RCTWebSocket', ], :inhibit_warnings => true

  2. pod install to install Yoga and React Native

  3. add RealmReact project into XCode from ../rn/node_modules/realm/react-native/ios/RealmReact.xcodeproj

  4. compile iOS app

Version: Realm 1.0.2 / Xcode 8.1 / OS X El Capitan 10.11.6

makeitnew commented 7 years ago

Try adding $(SRCROOT)/../../../../Pods/Headers/Public to the "Header Search Paths" for the RealmReact target.

To do this:

  1. Select the RealmReact project in your Xcode project
  2. Select the RealmReact Target
  3. Select "Header Search Paths" under the Build Settings tab
  4. Add the above path to the the list of existing search paths. The path above assumes both your Pods directory and your node_modules directory are at the top level of your project.
karapigeon commented 7 years ago

Hi @afces1. Thanks for reaching out about this. I recommend trying @makeitnew's suggestion but please let us know if that doesn't work or if you have any other questions.

fealebenpae commented 7 years ago

Hey @afces1,

I don't believe we support installing via CocoaPods - only with npm and the React Native CLI. I'll add this to our backlog.

In the mean time, the workaround proposed by @makeitnew seems viable.

natwales commented 7 years ago

Not having success with @makeitnew's suggestion. Not sure if this is because my Pods/Headers/ folder set in my search paths are in fact empty or if I am missing a step. Also it appears the '/ios' parent folder is missing in the search path suggested, though that didn't change anything for me. Would love to see a step by step breakdown of how to set this up so I could identify where maybe I was going wrong. This has been my approach:

1.npm install --save realm

2.drag RealmReact.xcodeproj file into my ReactNativeStarter.xcodeproj and include header search paths as described above and link libRealmReact.a framework. (alternativley react-native link realm seems to have the same effect???)

3.when I build I am getting an error that 'React/RCTBridgeDelegate.h' file not found in node_modules/realm/react-native/ios/RealmReact. (I notice that the Pods/Header/ folder is completely empty)

savelichalex commented 6 years ago

Hi, I'm also trying to install realm through cocoapods. Manual linking not helping in my case, because I'm using use frameworks! in my Podfile and that mean that React using as dynamic library. That also forces me to create podspec for realm. Right now I'm have something like this in realm folder on node_modules:

# coding: utf-8
require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
  s.name                    = 'RealmJS'
  s.version                 = package["version"]
  s.summary                 = package["description"]

  s.homepage                = "https://realm.io"
  s.source                  = { :git => 'https://github.com/realm/realm-js.git', :tag => "v#{s.version}", :submodules => true }
  s.author                  = { 'Realm' => 'help@realm.io' }
  s.library                 = 'c++', 'z'
  s.requires_arc            = true
  s.social_media_url        = 'https://twitter.com/realm'
  s.documentation_url       = "https://realm.io/docs/objc/#{s.version}"
  s.license                 = { :type => 'Apache 2.0', :file => 'LICENSE' }

  s.source_files            = 'react-native/ios/RealmReact/*.{h,mm}',
                              'src/*.{hpp,cpp}',
                              'src/ios/*.mm',
                              'src/jsc/*.{hpp,cpp}',
                              'src/object-store/src/*.{hpp,cpp}',
                              'src/object-store/src/sync/*.{hpp,cpp}',
                              'src/object-store/src/sync/impl/*.{hpp,cpp}',
                              'src/object-store/src/sync/impl/apple/*.{hpp,cpp}',
                              'src/object-store/src/impl/*.{hpp,cpp}',
                              'src/object-store/src/impl/apple/*.{hpp,cpp}',
                              'src/object-store/src/util/*.{hpp,cpp}',
                              'src/object-store/src/util/apple/*.{hpp,cpp}'

  s.ios.deployment_target   = '7.0'
  s.pod_target_xcconfig     = { 'APPLICATION_EXTENSION_API_ONLY' => 'YES',
                                'CLANG_CXX_LANGUAGE_STANDARD' => 'c++14' }

  s.dependency 'React'
end

But I got this error: impl/collection_change_builder.cpp:19:10: 'impl/collection_change_builder.hpp' file not found in src/object-store/src/impl folder. Looks like that problem with linking, but really I don't know how to solve it. I'm trying add 'USER_HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/RealmJS/src"' to s.pod_target_xcconfig and got Apple Mach-O Linker Error. Maybe someone know how to properly link it?

Also I want to ask, how Realm add constructor to js global namespace. I'm just walking around js codebase and found that in index.js file, Realm constructor should be defined yet.

kristiandupont commented 6 years ago

Realm constructor is added here: https://github.com/realm/realm-js/blob/master/src/jsc/jsc_init.cpp#L40

savelichalex commented 6 years ago

@kristiandupont Sorry for stupid question, but how this code run in application? I mean, what need to do (how link this file properly) to execute this code at runtime and add constructor to js context

kristiandupont commented 6 years ago

@savelichalex sorry, but I haven't tried building with cocoapods and we don't officially support it so I am not sure myself :-)

savelichalex commented 6 years ago

@kristiandupont I'm just trying to understand how it works and maybe this help me to understand how properly link it) I'm don't understand at which point in time jsc_init.cpp is calling and add constructor to js context. If rephrase: how initialization of library work

conanwsz commented 6 years ago

Also wish to see support Cocoapods.

lucaslz2020 commented 6 years ago

For iOS development, most developers use CocoaPods for dependency building. It's like android using Gradle for building. If a library does not provide CocoaPods or Carthage, I think it is not professional enough. For me, on the one hand really want to use realm-js, the other side need to use CocoaPods. So, I gave up realm. I thought it was only for half a year just because time was not enough. Come back, still do not support. Very disappointed.

allenhsu commented 6 years ago

FYI, CocoaPods v1.4.0 added support for script_phase, maybe it's possible to support CocoaPods now.

StevenMasini commented 6 years ago

It became some kind of convention to use Cocoapods to integrate React-Native dependencies for iOS. All my dependencies are using it, except Realm.

Is there any way we could move forward on this topic ?

aksswami commented 6 years ago

Any new updates on this?

arianito commented 6 years ago

i have the same problem, i can't use Cocoapods and realm at the same time, any changes ?

bmunkholm commented 5 years ago

We haven't had time to prioritise this. It would be wonderful if someone from the community would step up and help out on this one. Thanks!

yamiscott commented 5 years ago

Not that I have time to help, though I would love to:

I've recently run into a real world need for this.

Adding this to a brownfield app, but also wanting to access realm from the iOS and Android apps directly.

On Android, this is fine, no issues.

On iOS, the following cause linker (I sort of expected this) failures:

  1. Adding Realm (cocoa) via cocoapods, collision with realm. Also requires modular headers.
  2. Adding the static library along with the reactRealm library. Collisions occur.

I've basically parked this as I don't have enough resource to tackle this.

Cocoapods would solve this as the invisible dependencies of reactRealm with Realm would be fulfilled while also exposing Realm for usage within the non-RN side of the app.

githubdoramon commented 5 years ago

Is this ever going to be tackled? Anyone figured this out?

vdlindenmark commented 5 years ago

ReactNative is making cocoapods the default for iOS projects so it would be nice to see Realm also moved that way.

See this ticket for some more info from React Native: https://github.com/react-native-community/discussions-and-proposals/issues/104

vinhtnk commented 5 years ago

I have a solution: copy and import file libRealmReact.a into xcode project and build success.

maciekstosio commented 5 years ago

Hi @vinhtnk could you please provide steps how to import realm if I have just installed RealmJS? Do I need to add Realm to pods?

vinhtnk commented 5 years ago

Hi @vinhtnk could you please provide steps how to import realm if I have just installed RealmJS? Do I need to add Realm to pods?

Hi @maciekstosio the following steps:

maciekstosio commented 5 years ago

Thanks @vinhtnk for so quick answer. I believe I have linked libRealmJS.a but I'm not sure how to import it to my native code (native function called from RN), sorry if it's obvious but I don't really have iOS experience

vinhtnk commented 5 years ago

Thanks @vinhtnk for so quick answer. I believe I have linked libRealmJS.a but I'm not sure how to import it to my native code (native function called from RN), sorry if it's obvious but I don't really have iOS experience

Hi @maciekstosio, the RealmJS only use for RN imported to iOS project, if you want to use realm for native also, you need to use RealmSwift or RealmCocoa. And if you want communicate between RN and native please refer RN docs: https://facebook.github.io/react-native/docs/communication-ios

dnolbon commented 4 years ago

It's seems to work only without use_frameworks! option in Podfile

lebedev commented 4 years ago

Oh, I thought this issue was about supporting Realm as a pod. I second @dnolbon. For a while I have had working app with React Native installed with CocoaPods and Realm included as a library. Without use_frameworks! option if that matters. No other setting was required.

d-sandman commented 4 years ago
  • go to node_modules/realm/src/RealmJS.xcodeproj
  • build Realm project to libRealmJS.a
  • copy libRealmJS.a to current ios project directory
  • open current project and add the libRealmJS.a to project
  • at project target -> General -> Linked Frameworks and Libraries -> add libRealmJS.a
  • and now build successful without error

Were you able to access the same Realm file from React Native and from Swift/ObjC? Could you please show how your Podfile look like? I'm getting the same error again and again: .../libRealmReact.a(table.o) ld: 505 duplicate symbols for architecture x86_64

Thank you!

corradio commented 4 years ago

This is extremely helpful as we're currently unable to use realm with use_frameworks due to its inability to find React headers. We can include the React project but then we have duplicate symbols. The solution would be to only keep React inside Pods, and thus to migrate Realm to Pods as well. Is anyone working on this?

jaysquared commented 4 years ago

I slightly modified the Pods Header Search Path: $(SRCROOT)/../../../../ios/Pods/Headers/Public/React-Core

Otherwise I would get compiler errors around time.h

I also removed GCDWebServer from here, as I have it in another dependency RealmReact.xcodeproj/Libraries/RealmJS.xcodeproj/Vendor/GCDWebServer

Now it builds and runs without issues.

esutton commented 4 years ago

Anyone have a realm fork with a podspec supporting react native 0.60?

Out of the many react native packages my app uses, realm is the only one to NOT have a podspec yet.

Work-around:

Call this from your package.json postinstall script.

echo "Create podspec for realm"
cat > ./node_modules/realm/realm.podspec << ENDOFFILE
require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
  s.name         = package['name']
  s.version      = package['version']
  s.summary      = package['description']
  s.license      = package['license']

  s.authors      = package['author']
  s.homepage     = package['homepage']
  s.platform     = :ios, "7.0"

  s.source       = { :git => "https://github.com/realm/realm-js.git", :tag => "#{s.version}" }
  s.source_files  = "ios/*.{h,m}"

  s.dependency 'React'
end
ENDOFFILE

Add this to your /ios/Podfile

  pod 'realm', :path => '../node_modules/realm'  

Never mind.

Now I have a run time error missing realm-constructor.

How can realm be used in a react native 0.60 project that is using podfiles?

2019-08-12 11:45:43.530 [error][tid:com.facebook.react.JavaScript] Missing Realm constructor. 
Did you run "react-native link realm"? 
Please see https://realm.io/docs/react-native/latest/#missing-realm-constructor for troubleshooting
rudyryk commented 4 years ago

@esutton Seems like we need more than just s.source_files = "ios/*.{h,m}"

And btw I'm also here:

Out of the many react native packages my app uses, realm is the only one to NOT have a podspec yet.

20+ other libs but only for Realm there's no podspec.

esutton commented 4 years ago

@rudyryk I tried changing my realm.podspec to be more inclusive without success to s.source_files = "**/*.{h,m}".

Since upgrading to react native to 0.60.4, I have everything building using pods except for realm js.

Dexwell commented 4 years ago

Until this is implemented, this is how to get your project to build: https://github.com/realm/realm-js/issues/2431#issuecomment-510742772

Drag RealmReact.xcodeproj to Libraries, then add both libRealmReact.a and libRealmJS.a to Linked Frameworks and Binaries.

MobileAppVault commented 4 years ago

I can confirm. RN 0.60 and Realm is working with below manual steps

1. Manual linking RealmReact

Drag RealmReact.xcodeproj to Libraries, 

then add both libRealmReact.a and libRealmJS.a to Linked Frameworks and Binaries

#2431 comment

2. modify header search patch

I slightly modified the Pods Header Search Path:
$(SRCROOT)/../../../../ios/Pods/Headers/Public/React-Core

Otherwise I would get compiler errors around time.h

I also removed GCDWebServer from here, as I have it in another dependency
RealmReact.xcodeproj/Libraries/RealmJS.xcodeproj/Vendor/GCDWebServer
OceanHorn commented 4 years ago

@MobileAppVault It works in most situations, but won't work when needing to enable use_frameworks! configuration.

harunsmrkovic commented 4 years ago

Inability to link Realm via CocoaPods (with use_frameworks!) option is what's preventing me to use it in my project... Really hope to see this resolved 🤞

agustin107 commented 4 years ago

I can confirm. RN 0.60 and Realm is working with below manual steps

1. Manual linking RealmReact

Drag RealmReact.xcodeproj to Libraries, 

then add both libRealmReact.a and libRealmJS.a to Linked Frameworks and Binaries

#2431 comment

2. modify header search patch

I slightly modified the Pods Header Search Path:
$(SRCROOT)/../../../../ios/Pods/Headers/Public/React-Core

Otherwise I would get compiler errors around time.h

I also removed GCDWebServer from here, as I have it in another dependency
RealmReact.xcodeproj/Libraries/RealmJS.xcodeproj/Vendor/GCDWebServer

This works for me! Thanks @MobileAppVault

kmvkrish commented 4 years ago

Try adding $(SRCROOT)/../../../../Pods/Headers/Public to the "Header Search Paths" for the RealmReact target.

To do this:

  1. Select the RealmReact project in your Xcode project
  2. Select the RealmReact Target
  3. Select "Header Search Paths" under the Build Settings tab
  4. Add the above path to the list of existing search paths. The path above assumes both your Pods directory and your node_modules directory are at the top level of your project.

This Header_Search_Path is already available for RealmReact.xcodeproj. But the build is failing with <React/RCTBridge+Private.h> file not found error. This is occurring with React Native version 0.60+

kneth commented 4 years ago

In Realm JavaScript v3.4.0 we have added support for CocoaPods.

theminerymike commented 4 years ago

@kneth That's great to hear, but if I understand this issue properly (which I'm currently still stuck on), wouldn't we need a podspec for RealmReact as well? I'm having issues building RealmReact, not RealmJS.

kraenhansen commented 4 years ago

@theminerymike - it could probably be separated into its own Subspec, but currently the RealmJS Podspec builds both Realm JS (the former libRealmJS) and Realm React into a single library.

I were the one fixing this issue and to be honest, this was my first Podspec. Any feedback on it would be greatly appreciated. Do you see an issue in compiling everything to a single library instead of separating it into Realm JS and Realm React?

theminerymike commented 4 years ago

Oh cool, that should do the trick. I wasn't aware of that. What I've done so far is adding this line to my Podfile, and unlinked the two realm libraries in Xcode, like I've done with other projects that now use cocoapods.

pod 'RealmJS', :path => '../node_modules/realm'

It seems to fail for me on line 21 of object-store/src/index_set.cpp

Screen Shot 2019-11-23 at 8 35 09 PM

realm/util/assert.hpp not found

Any advice?

kraenhansen commented 4 years ago

Any advice?

@theminerymike I suspect your Podfile needs some adjustments: In general it should not need to reference RealmJS explicitly. You could have a look at the Podfile of a newly initialized React Native project. If that doesn't work, please create a new issue and posting your Podfile there.