Skip to content

Latest commit

ย 

History

History
205 lines (140 loc) ยท 7.05 KB

Realm.md

File metadata and controls

205 lines (140 loc) ยท 7.05 KB

Realm

Realm์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

-Realmํ…Œ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์˜คํ”ˆ ์†Œ์Šค๋กœ ๋ชจ๋ฐ”์ผ ์‚ฌ์šฉ์— ์ดค์ ํ™”๋œ ๋‚ด์žฅ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

Realm์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

  • ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ณด๋‹ค ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.
  • ORM*๊ณผ ๊ฐ™์€ ๋›ฐ์–ด๋‚œ ์‚ฌ์šฉ์„ฑ
  • ์˜คํ”ˆ์†Œ์Šค๋ผ์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์›€
  • ์ฝ”๋“œ๊ฐ€ ๊ฐ„๋‹จํ•จ

์šฉ์–ด์ •๋ฆฌ

  • ORM(Object-relational mapping): ๋‹จ์ˆœํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๋ฉด ๊ฐ์ฒด์™€ ๊ฐ์ฒด ๊ฐ„์— ๊ด€๊ณ„์˜ ์„ค์ •

    ORM์—์„œ ๋งํ•˜๋Š” ๊ฐ์ฒด์˜ ์˜๋ฏธ๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์•Œ๊ณ  ์žˆ๋Š” OOP(Object Oriented Programming)์˜ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. OOP๋Š” ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ์„ ๋งํ•˜๋ฉฐ ์—ฌ๊ธฐ์—์„œ์˜ ๊ฐ์ฒด๋Š” ์ €์žฅ๊ณต๊ฐ„์—์„œ ํ• ๋‹น๋˜์–ด ๊ฐ’์„ ๊ฐ€์ง€๊ฑฐ๋‚˜ ์‹๋ณ„์ž์— ์˜ํ•ด ์ฐธ์กฐ๋˜๋Š” ๊ณต๊ฐ„์„ ์˜๋ฏธ ํ•œ๋‹ค.

์ฐธ๊ณ 

http://cocomo.tistory.com/409 sqlLite ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

Realm list

Realm list๋Š” ๋ณด๋ฐ”์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค Api์ค‘ ํ•˜๋‚˜์ด๋‹ค. listํด๋ž˜์Šค๋Š” ๊ฐ์ฒด๊ฐ„์˜ ์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

####realm ๊ณผ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋น„๊ต

sql

์œ„์— ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋ณต์žกํ•œ ๊ด€๊ณ„๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ผ๋ฉด ๋ช‡ ๊ฐœ์˜ ๊ธฐ๋Šฅ๋งŒ ์ถ”๊ฐ€ํ•˜๋”๋ผ๋„ ๋” ์–ฝํžˆ๊ฒŒ ๋œ๋‹ค.

๋‘๋ช…์˜ ์‚ฌ์šฉ์ž๊ฐ€ ์ž‡๋Š” ๊ฒฝ์šฐ ๊ทธ๋“ค์˜ ์ •๋ณด๊ฐ€ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด, user์™€ repositories ๋‚ด์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ๋งํฌํ•ด์„œ ์ฐพ์•„ ๋‚ด์•ผ ํ•œ๋‹ค . ๊ฒŒ๋‹ค๊ฐ€ ์ด๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ ๋งˆ๋‹ค ์‹ค์ œ๋กœ ๊ฐ€์ ธ์˜ฌ ๋‘๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋งค๋ฒˆ ์ฐพ์•„๋‚ด๋Š” ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค. ํ•œ๋ˆˆ์— ๋ด๋„ ๊ทธ๋ ‡๊ฒŒ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ join๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ถŒํ•œ์„ ๋”ฐ๋กœ ๋ถ€์—ฌํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ Realm๋ชจ๋ฐ”์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ํ…Œ์ด๋ธ”์„ joinํ•˜์ง€ ์•Š์œผ๋ฉด sql ์ฟผ๋ฆฌ๋„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. Realm ๋ชจ๋ฐ”์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค. ํ•œ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ๋•Œ ๋ถ€๋ชจ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ž์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ํฌ์ธํ„ฐ๋งŒ ์œ ์ง€ํ•œ๋‹ค.

realm

List๋Š” ์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ์ •๋ง ํšจ์œจ์ ์œผ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค. Array์™€ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ฒŒ ์ž‘๋™ํ•˜๋ฉฐ, ๊ฐ์ฒด๊ฐ€ ์ถ”๊ฐ€๋œ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๊ณ , ๊ฐ์ฒด๋ฅผ ํŠน์ • ์ธ๋ฑ์Šค์— ์ถ”๊ฐ€,์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜ ์ด๋™ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋„ ์ œ๊ณตํ•œ๋‹ค. List์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๊ฐ์ฒด ์ž์ฒด๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ง์ ‘์ ์ธ ์ธ๋ฑ์Šค ๋ชฉ๋กœ๋งŒ ์ €์žฅํ•œ๋‹ค.

Realm ๊ฐ์ฒด์ƒ์„ฑ

Realm์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” realm.createObject๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” *Proxy๊ฐ์ฒด๋กœ ๋งŒ๋“œ๋Š” ๊ฐœ๋…์ด๋‹ค.

Realm ๊ฐ์ฒด๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๋‚ด๋ถ€์ ์œผ๋กœ ํ…Œ์ด๋ธ” ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋น„์–ด์žˆ๋Š” row๋ฅผ ๋งŒ๋“ค๊ณ  ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค. realm์—์„œ ์‚ฌ์šฉํ•˜๋Š” Schemam๋Š” ์ผ์ข…์˜ *๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์ด์ž ๋ชฐ๋ธ๊ณผ ์—ฐ๊ด€๋œ reamlObjectSchema์™€ table์„ ๊ด€๋ฆฌํ•œ๋‹ค. ํ…Œ์ด๋ธ” ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ํ˜ธ์ถœ ํ•ด์„œ ๋ฉ”์„œ๋“œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ ์ง์ ‘ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  Poxy๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

๊ฐ์ฒด

Realm java ๊ฐ์ฒด๋Š” Proxy ๊ฐ์ฒด๋ฅผ ๊ฑฐ์น˜๊ณ  JNI๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ Table.cpp๋ผ๋Š” C++๋กœ ๋งŒ๋“ค์–ด์ง„ ์ฝ”์–ด ๊ฐ์ฒด ์•ˆ์— ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.

public long addColumn(RealmFieldType type, String name, boolean isNullable) {
  verifyColumnName(name);
  return nativeAddColumn(nativePtr, type.getNativeValue(), name, isNullable);
}

Table.java์˜ ์‹ค์ œ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด addCoumn์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ JNI์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” nativeaddColumn ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ์ด๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด C++๋กœ ๋œ JNI๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ , ์‹ค์ œ๋กœ Table.cpp๋ฅผ ํ˜ธ์ถœํ•ด์„œ add_column๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

์šฉ์–ด

Proxy: ์‹ค์ œ๋กœ ์•ก์…˜์„ ์ทจํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋Œ€์‹ ํ•ด์„œ ๋Œ€๋ฆฌ์ž ์—ญํ• ๋ฅผ ํ•ด์ค€๋‹ค. ํ”„๋ก์‹œ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•„์š”์— ๋”ฐ๋ผ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚ค๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๋Š” ์ด์ ์ด ์ƒ๊ธด๋‹ค.

๋ฉ”ํƒ€๋ฐ์ดํ„ฐ: ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ˆ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

android realm ์˜ˆ์ œ

gradle_android

๋จผ์ € ์œ„์˜ ์‚ฌ์ง„์— ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:5.3.1"
    }
}

gradle_app

์ถ”๊ฐ€ํ•œ ๋‹ค์Œ build.gradle(app)์— ๊ฐ€์„œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋งจ ์•„๋ž˜์— ์ถ”๊ฐ€ํ•œ๋‹ค.

apply plugin: 'realm-android'

๋ชจ๋ธ ํด๋ž˜์Šค ์ •์˜

public class Dog extends RealmObject {
    private String name;
    private int age;
	    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

Realm๋ฐ์ดํ„ฐ ์“ธ๋•Œ ๊ผญ ํ•ด์ค˜์•ผํ•˜๋Š” ๊ฒƒ

//realm ์ดˆ๊ธฐํ™”
Realm.init(context);

//์ด ์Šค๋ ˆ๋“œ์— ๋Œ€ํ•œ Realm ์ธ์Šคํ„ด์Šค ๊ฐ€์ ธ์˜ค๊ธฐ
Realm realm = Realm.getDefaultInstance();

insert

Realm mRealm;
// ํŠธ๋žœ์žญ์…˜์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์˜์†ํ™”ํ•ฉ๋‹ˆ๋‹ค
mRealm.beginTransaction();
Dog dog=mRealm.createObject(Dog.class);
dog.setName("๋ฉ๋ฉ์ด");
dog.setAge(3);
mRealm.commitTransaction();

Select

Realm mRealm;

//select All
RealmResults<Dog> result=mRealm.where(Dog.class).findAll();
for(int i=0;i<result.size();i++){
  Dog dog=result.get(i);
  dog.getName();
  dog.getAge();
}
//db์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ฒซ๋ฒˆ์งธ๊บผ select
Dog dog=mRealm.where(Dog.class).findFirst();
dog.getName();
dog.getAge();
//db์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฒƒ ์ค‘์— ์ž์‹ ์ด ์›ํ•˜๋Š” ๊ฑฐ ์ถœ๋ ฅ
Dog dog=mRealm.where(Dog.class).equalTo(name,"๋ฉ๋ฉ์ด").findFirst();
dog.getName();
dog.getAge();

Delete

 Realm();
 mRealm.beginTransaction();
 RealmResults<DB> results=mRealm.where(DB.class).findAll();
 results.deleteAllFromRealm();//์ „๋ถ€ ์‚ญ์ œ
 mRealm.commitTransaction();
 Realm();
 mRealm.beginTransaction();
 RealmResults<DB> results=mRealm.where(DB.class).findAll();
 results.deleteFromRealm(0); 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ ์‚ญ์ œ
 mRealm.commitTransaction();

*๊ฐ€์žฅ ๋‚˜์ค‘์— ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๊ฐ€ 0๋ฒˆ์งธ์ž„

Between

Realm();
RealmResults<DB> results=mRealm.where(DB.class).between("age",start,finish).findAll();
 for(int i=0;i<results.size();i++){
    DB db=results.get(i);
   Toast.makeText(getApplication(),"์ด๋ฆ„:"+db.getName()+" 
   ๋‚˜์ด:"+db.getAge()+"๊ฐœ์ˆ˜:"+String.valueOf(i+1),Toast.LENGTH_LONG).show();

                }

์ถœ์ฒ˜:

https://academy.realm.io/kr/posts/realm-api-optimized-for-performance-and-low-memory-use/ (realm ๋ฆฌ์ŠคํŠธ)

https://academy.realm.io/kr/posts/realm-api-optimized-for-performance-and-low-memory-use/ (realm object ์„ค๋ช…)

https://realm.io/kr/docs/java/latest/ (realm ์˜ˆ์ œ)