Core Data VS Sqlite or FMDB…?

后端 未结 6 659
广开言路
广开言路 2020-12-07 10:40

Now this might look like a duplicate thread, but my question is that I have read a lot of questions like.. Core Data vs SQLite 3 and others but these are 2-3 years old. I ha

6条回答
  •  -上瘾入骨i
    2020-12-07 11:21

    I recently embarked on this journey myself, and ended up trying out all three. Here's what I learned:

    • Raw sqlite3
      • Low-level, full access to database. No abstractions. Very verbose - it takes a good deal of code to do very simple things.
    • Core Data
      • Very high-level, built on abstractions, MUST use Apple's generated database. Useful for iCloud synchronization and simple iOS-only data management. Difficult and dangerous to directly access database, and should not be used for cross-platform databases. Still takes a fair amount of code to do simple things.
    • FMDB
      • High-level, very abstraction friendly but not forced. Still get full access to database if you need it. Provides an NSDictionary of the result with each item automatically typecasted to the mutable variant of the proper datatype (e.g., text columns are returned as NSMutableString). I ended up building a very simple wrapper class around it to abstract it even more, so I have a helper class with static functions like selectAllFrom:(NSString *)table where:(NSDictionary *)conditions, which returns an NSArray of NSDictionary objects. It's fantastic to be able to do things like NSArray *usersNamedJoe = [DBHelper selectAllFrom:@"user" where:@{@"name": @"Joe"}];.

    Basically, while Core Data may be useful for simple iOS-only apps, anyone who's interested in using cross-platform databases should stay far, far away from it - Apple has no interest in making that easy, and it shows.


    TL;DR:

    • Don't use raw sqlite3 unless you're doing something extremely trivial.
    • Core Data is fine for simple iOS-only data, if you're comfortable with being locked into it.
    • If you want full control over the database and you're not doing something trivial, or you're building your app for multiple platforms, FMDB is definitely the way to go.

提交回复
热议问题