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.
-
Create a MOE module then sync your gradle.
-
Open terminal and cd to xcode directory.
-
Close Xcode and be sure to it completely closed.
-
Install pod from Install CocoaPods 0.39.0 or later. and then run
pod init
to create Podfile for you. -
In your Podfile, add pod ‘Realm’ to your app target and pod ‘Realm/Headers’ to your test target.
-
From the command line, run pod install.
-
Use the .xcworkspace file generated by CocoaPods to work on your project!
-
You can see more detail from this link about realm database CocoaPod.
-
In android studio open ios module gradle file and uncomment OR add following code under moe -> xcode
workspace ‘xcode/Sample.xcworkspace’
mainScheme ‘Sample’
testScheme ‘Sample-Test’
-
Sync your gradle again.
-
Right click on ios module under
Multi-OS-Engine
click oncreate new binding
-
Click on
icon set
Name: Realm
and ChooseFramework
type then click OK -
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” -
In android studio click on
then Generate Bindings (after a freezing android studio you will see Generate successfully)
-
Now you need to create you’r object and save them on database.
-
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
-
In AndroidStudio right click on ios module under
Multi-OS-Engine
click oncreate new binding
-
Click on
icon set
Name: Person
and ChooseHeader
type then click OK -
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” -
click on
then Generate Bindings (after a freezing android studio you will see Generate successfully)
-
Modify RLMObject.h, RLMResults.h and undo change’s, save again and close it.
-
Open RLMRealm.java in android studio and modify defaultRealm method to:
public static native RLMRealm defaultRealm();
- 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();
- 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.