Using Realm on MOE

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