2020년 7월 27일 월요일

Using Swift framework in an Objective-c project

reference
https://marcinczenko.github.io/post/using-swift-frameworks-from-objective-c-target/


===============================================
=============== Swift Framework ===============
===============================================
Make a ExampleSwiftFramework framework. not Static Library
Add ExampleSwiftFramework.swift and put the code below

@objc
public class ExampleSwiftFrameworkClass : NSObject {

    let message: String

    @objc(init:)
    public init(withHelloMessage message: String) {
        self.message = message
    }

    @objc(sayHello)
    public func sayHello() -> String {
        return self.message
    }
}

TARGETS->Build Settings
Mach-O : 'Dynamic Library'

Build Options->Always Embeded Swift Standard Libraries : No
Packaging->Defines Module : Yes
Apple Clang-Language-Modules->Enable Modules(C and Objective-C) : Yes

next to the upper left 'stop' icon. click framework and edit scheme->Run->Build Configuration=> Release

Click Product->Build
and you can see the built framework on the project navigato.
Right click at the file->Show in Finder

===============================================
============= Objective-C project =============
===============================================
Make an objective-C project

TARGETS->General
1. add the swift framework into the 'Frameworks, Libraries, and Embedded Content'
2. and then you can see 'Framework' folder in the project navigator.
3. remove the framework from the 'Frameworks, Libraries, and Embedded Content'
4. drag the framework.framework folder into the 'Framework' in the project navigator

TARGETS -> Build Settings(All, Combined taps)
Build Option -> Always Embed Swift Standard Libraries : No

TARGETS -> Build Phases
Click '+' and add New Copy Files Phase category
add the swift framework what you need and keep the default option(Code Sign On Copy : checked, Copy only when installing : unchecked)

put the code below in -(void)viewDidLoad

ExampleSwiftFrameworkClass *exam = [[ExampleSwiftFrameworkClass alloc] init:@"Test"];
NSLog(@"%@", [exam sayHello]);

2020년 7월 19일 일요일

Room migrations in Kotlin

To test you need two class 

//===============================================================
// AppDatabase class
Database(entities = [Song::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
        companion object {
        @Volatile private var INSTANCE: AppDatabase? = null
        fun getInstance(context: Context): AppDatabase =
            INSTANCE ?: synchronized(this) {                
         INSTANCE ?: buildDatabase(context).also {INSTANCE = it }}
        private fun buildDatabase(context: Context) =              
        Room.databaseBuilder(context.applicationContext,                  
        AppDatabase::class.java, "test.db").build()
        fun destroyInstance() {
            INSTANCE = null        
        }
    }
}

//===============================================================
// Entity class
// when the version was 1
@Entitydata class Song(
    @PrimaryKey val id: Long,    val title: String
)
//===============================================================


When you run an app with the database version 1
after then you modified the Song class added 'tag' entity.
@Entitydata class Song(
    @PrimaryKey    val id: Long,    
    val title: String,    
    val tag: String // Added in version 2.
)
you should change the version number of AppDatabase class like below from 1 to 2
Database(entities = [Song::class], version = 2, exportSchema = false)

you also have to edit database builder like this

private fun buildDatabase(context: Context) =
     Room.databaseBuilder(context.applicationContext, 
          AppDatabase::class.java, "terry.db")
          .addMigrations(MIGRATION_1_2).build()

and add migration code or edit it in the Song class
// Migration from 1 to 2, Room 2.1.0val MIGRATION_1_2 = object : Migration(1, 2) {
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL(
            "ALTER TABLE Song ADD COLUMN tag TEXT NOT NULL DEFAULT ''")
    }
}

That's all! :)