Using Realm on MOE

In this case MOE 2.x will support Onyx. You can expect the first alpha soon after the MOE 1.3 release.

Dear Gergely,

I’m really thankful about you’re fast replies and best supporting of MOE.
Whoever iam force to implement this API to our project.
If i getting closer to my goal i will put here a good step by step guide here to used for anyone.

Best Regards,
Saeed

Thanks Gergely, I will await the alpha then.

Dear @kisg , @blueneogeo and all friends,

I have worked on Realm database and finally i found a good way to connect Realm database to our MOE application’s.
at first it’s necessary to thankful from my friend Gergely about best supporting of MOE.

be sure you’re using MOE 1.3 and later

please follow all steps one by one and be careful to don’t forget any things.

  1. Create a MOE module then sync your gradle.

  2. Open terminal and cd to xcode directory.

  3. Close Xcode and be sure to it completely closed.

  4. Install pod from Install CocoaPods 0.39.0 or later. and then run pod init to create Podfile for you.

  5. In your Podfile, add pod ‘Realm’ to your app target and pod ‘Realm/Headers’ to your test target.

  6. From the command line, run pod install.

  7. Use the .xcworkspace file generated by CocoaPods to work on your project!

  8. You can see more detail from this link about realm database CocoaPod.

  9. In android studio open ios module gradle file and uncomment OR add following code under moe -> xcode :heart_eyes:

workspace ‘xcode/Sample.xcworkspace’
mainScheme ‘Sample’
testScheme ‘Sample-Test’

  1. Sync your gradle again.

  2. Right click on ios module under Multi-OS-Engine click on create new binding

  3. Click on :heavy_plus_sign: icon set Name: Realm and Choose Framework type then click OK

  4. Fill all textfield’s like this:
    Framework path: realm-objc-2.4.3/ios/dynamic/Realm.framework
    Base package name: com.example.sample.pods.realm
    Import Header:
    #import “Realm/Realm.h”

  5. In android studio click on :gear: then Generate Bindings (after a freezing android studio you will see Generate successfully)

  6. Now you need to create you’r object and save them on database.

  7. Open Xcode and create a cocoa touch (in this tutorial i will name’d it Person)
    Modify Person.h to

    #import <Foundation/Foundation.h>
    #import "RLMObject.h"
    @interface Person : RLMObject
    @property NSString *name;
    @property BOOL adopted;
    @end
  1. In AndroidStudio right click on ios module under Multi-OS-Engine click on create new binding

  2. Click on :heavy_plus_sign: icon set Name: Person and Choose Header type then click OK

  3. Fill all textfield’s like this:
    Header path: xcode/Sample
    Base package name: com.example.sample.model
    Explicit library: model
    Import Header:
    #import “Person.h”

  4. click on :gear: then Generate Bindings (after a freezing android studio you will see Generate successfully)

  5. Modify RLMObject.h, RLMResults.h and undo change’s, save again and close it.

  6. Open RLMRealm.java in android studio and modify defaultRealm method to:

    public static native RLMRealm defaultRealm();
  1. Open Person.java in android studio and add two following method’s:
    @Selector("allObjects")
    public static native RLMResults<Person> allObjects();
    @Generated
    @Selector("init")
    public native Person init();
  1. Finally done.

here a method for save and read result:

    public void save() {
        RLMRealm realm = RLMRealm.defaultRealm();
        Person person = Person.alloc().init();
        person.setName(name.text());
        person.setAdopted(false);
        realm.beginWriteTransaction();
        realm.addObject(person);
        realm.commitWriteTransaction();
    }

    public void showAll() {
        RLMResults<Person> results = Person.allObjects();

        for (int i = 0; i < results.count(); i++) {
            Person person = (Person) results.objectAtIndex(i);
            Foundation.NSLog("person : " + person.name(), "");
        }
    }

Best Regards,
Saeed.

4 Likes

Dear @saeed.motevalian.
Thank you for your description :slight_smile:

I’ve tried to connect Realm to my MOE apps according to yours steps but it doesn’t work.
Firstly, I had problem to bind Realm to Java.

When I fulfilled nbc file according to your descrption, I got error:

fatal error: ‘Realm/Realm.h’ file not found

Finally I bound my code using this fields:

Header path: “xcode/Pods/Realm”

Header search paths: “xcode/Pods/Headres
xcode/Pods/Headres/Private
xcode/Pods/Headres/Public”

Import headers: "#import “Realm/Realm.h”

Unfortunately, I cannot create object to save it to db.
When I try to create Person, Xcode shows error

“RLMObject.h” file not found

What am I doing wrong?

Dear @Wewe,

I had a tiny mistake on typing and fix it right now.

Check again when you add nbc file choose framework then select your framework path

Regards,
Saeed

Dear @saeed.motevalian

It still doesn’t work. I got error:
/Realm.framework/Headers/Realm.h:21:9: error: 'RLMArray.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/RLMArray.h:21:9: error: 'RLMCollection.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:22:9: error: 'RLMMigration.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/RLMMigration.h:74:70: warning: unknown attribute 'noescape' ignored [-Wunknown-attributes] /Realm.framework/Headers/Realm.h:23:9: error: 'RLMObject.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/RLMObject.h:21:9: error: 'RLMObjectBase.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/RLMObject.h:22:9: error: 'RLMThreadSafeReference.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:24:9: error: 'RLMObjectSchema.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:25:9: error: 'RLMPlatform.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:26:9: error: 'RLMProperty.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/RLMProperty.h:20:9: error: 'RLMConstants.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:27:9: error: 'RLMRealm.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/RLMRealm.h:296:46: warning: unknown attribute 'noescape' ignored [-Wunknown-attributes] /Realm.framework/Headers/RLMRealm.h:318:46: warning: unknown attribute 'noescape' ignored [-Wunknown-attributes] /Realm.framework/Headers/Realm.h:28:9: error: 'RLMRealmConfiguration.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:29:9: error: 'RLMRealmConfiguration+Sync.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:30:9: error: 'RLMResults.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:31:9: error: 'RLMSchema.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:32:9: error: 'RLMSyncConfiguration.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:33:9: error: 'RLMSyncCredentials.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:34:9: error: 'RLMSyncManager.h' file not found with <angled> include; use "quotes" instead /Realm.framework/Headers/Realm.h:35:9: error: 'RLMSyncPermission.h' file not found with <angled> include; use "quotes" instead fatal error: too many errors emitted, stopping now [-ferror-limit=]

Best Regards

Dear @Wewe,

It seems that you have a problem on nbc file
Please check the frameworks path
I hope you can find your problem.
Please create new projects and follow all steps agin.(don’t forget u need to use pod for xcode and framework for nbc file)

Regards,
Saeed

Is this the recommended approach to use Realm in MOE?

Just to be clear, if Realm had a pure Java implementation I could have the entire data layer in the commons module and reuse it for both platforms?

Dear @jirungaray,

Realm based on android.
Iam searching alot and finally know that :I need to put this api in both modules.

Regards,
Saeed.

@saeed any chance you can share your binding files?

follow this link

After the CocoaPods install, I do not have a framework anywhere like Saeed mentions:

I do have a bunch of h files in an “include” directory inside the Realm Pods directory. But, it doesn’t look like the pod install installs a framework.

Did you happen to download that separately?

I don’t know how you went around doing things, I’ll quickly detail how I personally installed the SDK we are using for our iOS version :

/!\ During all these steps, always have the Xcode project closed. When you open it to modify something (for example config files), close it when you’re done.

  • Add the pod to your Podfile
  • If this is the first time you’re using CocoaPods, make sure to change, in the Xcode project, the config files (for debug and release) to “None”. Else you’ll have conflicts with CocoaPods.
  • pod install
  • This will have set the config files, in Xcode, to the ones from CocoaPods, that’s good.
  • If you’re using LibGDX or any other framework that requires you to use a custom xcconfig file (that’s our case), modify your Pods xcconfig files (xcode/Pods/Target Support Files/Pods-ios-moe/Pods-ios-moe.debug.xcconfig , same for release) and add #include "../../../ios-moe/custom.xcconfig"at the top (this way, even though the Xcode project uses the Pods config file, the Pod config file itself has a reference to your custom xcconfig file, which is needed for LibGDX for example. Be careful, you need to re-do this EVERY time you use pod install, because it overwrites the Pods xcconfig file.)
  • Open the .xcworkspace file (when you’re using CocoaPods with Xcode, you need to use this file instead of the xcproject file to work with Xcode), click on Products > Schemes > Manage Schemes, and make sure all schemes are ticked as “shared”. Close Xcode.
  • At this point, your MOE project should properly compile and run in your IDE. If it doesn’t, there’s a problem somewhere.
  • Now you need to do the bindings. Right click your MOE module in your IDE, Multi-OS Engine Actions > Create new bindings.
  • Name it “Realms.nbc” for example.

Here is an example of one of our NBC files, for the Facebook SDK :

48

  • For “output directory”, put where your sources are.
  • Click the green “+”, name it Realms for example. Select “Headers”, since from what I’ve read, you don’t have a .framework file.
  • For “Header path”, “Header search paths”, “User header search paths” and “Framework search paths” you can use the same path, the xcode/Pods/<name_of_the_library>. In your case, that would be xcode/Pods/Realms I imagine ?
  • In “Import headers”, pick the specific headers you need bindings for. You can put all the headers if you wish, although that might be a big too big/long to write. Only use quotes, not < or >. For example in our case, the Facebook SDK has a shit ton of headers, we only need one for the Core SDK, which is FBSDKCoreKit.h. If you realize you’re missing headers, you can always modify the .nbc file and regenerate the bindings, it will seamlessly re-generate the previous bindings (so no code will break) + add the new ones.

That’s it I think. I hope this applies to your case, I think so since you don’t seem to have any framework but just headers/sources, in any case it’s the precise steps to follow whenever you want to add a CocoaPods pod.

you need to download framework separately.

Hi @saeed, I am trying to follow your instructions but am slightly puzzed by the step in the quote.
How should RLMObject.h and RLMResults.h be modified? What changes should be undone?

Thanks in advance.

Lara

Dear @laranyman,

This step needs to remove. I will ask MOE admin to gimme access to edit this post.

Regards,
Saeed.

Ah ok, thanks @saeed.

I was following the instructions and managed to complete most of them (after importing Realm.framework which wasn’t mentioned in the original instructions but found a separate comment mentioning this addition).

However, I am having some issues with generating the bindings for the Person RLMObject as per step:

click on :gear: then Generate Bindings (after a freezing android studio you will see Generate successfully)

When attempting to generate the bindings, I get the following error:

...ios/xcode/ios/Person.h:10:9: fatal error: 'RLMObject.h' file not found

I have tried different combinations of imports in Person.h file to the above mentioned such as:

#import <Foundation/Foundation.h>
#import <Realm/RLMObject.h>

but unfortunately this has not worked either. Was wondering if you might be able to help/have any ideas?

Thanks

Dear @laranyman,

You need to put this binding in realm binding and put it on below of realm than when u generate all binding everything worked ok.

54 AM

Regards,
Saeed.

1 Like

Thank you @saeed for all your help! That worked - I had the Entity (Person binding) in a different binding file which caused my issue.