V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
ben46
V2EX  ›  iDev

Coredata 太**难用了, 于是我写了这个框架, 搞定 json 解析&数据的增删改查

  •  
  •   ben46 · 2014-09-13 09:52:24 +08:00 · 4835 次点击
    这是一个创建于 3549 天前的主题,其中的信息可能已经有所发展或是发生改变。

    于是我写了这个框架, 它可以很超简单地用于json解析和sqlite 增删改查。

    这个库是在JSONModel基础之上做的.

    JSONModel是用来解析json的一个库, 解析json完成之后, 我们需要做数据存储, 怎么办?

    • 方法1. 存储返回的json到NSUserDefaults里面, 但是这样做update或者select的时候就蛋疼了

    • 方法2. 自己用sqlite存起来, 但是这样需要自己把每个字段先建好, 每个参数insert的时候, 都需要写一段sql, 太麻烦!

    • 方法3. 使用coredata, 但是coredata却又有众所周知超难用的地方, 并且不适合数据量大了之后使用.

    这些方法都糟糕透了!

    于是我写了这个框架, 融合了json解析, 巨简单的sqlite增删改查.

    项目地址:
    https://github.com/ben46/jsonmodel

    如何安装:
    pod 'JSONModel', :git => 'https://github.com/ben46/JSONModel.git'


    如何使用:

    服务器返回的JSON:

    {
      "ID": "1",
      "name": "Product name",
      "price": 12.95
    }
    

    定义你的数据model

    @interface ProductModel : JSONModel
    @property (assign, nonatomic) NSNumber<JMPrimaryKey> *ID;
    @property (strong, nonatomic) NSString* name;
    @property (assign, nonatomic) float price;
    @end
    

    某个你的代码处 :

    #import "CountryModel.h"
    ...
    
    NSString* json = (fetch here JSON from Internet) ... 
    NSError* err = nil;
    
    CountryModel* country = [[CountryModel alloc] initWithString:json error:&err];
     // 自动建表 && 自动生成sql && 自动保存数据
    [country JM_save];
    
    // find the value matches the primary key
    CountryModel* countryYouJustSaved = [CountryModel JM_find:@1]; 
    NSLog(@"%@", [countryYouJustSaved toDictionary]);
    

    欢迎大家提issue, 我会在第一时间修复.

    5 条回复    2014-09-14 16:53:22 +08:00
    zichen0422
        1
    zichen0422  
       2014-09-13 10:58:25 +08:00
    赞楼主创作.
    yellowV2ex
        2
    yellowV2ex  
       2014-09-13 20:42:32 +08:00
    曾用过 SQLitePersistentObject 被坑了,这东西是自动把SQLite转为Object,也是超简单的查询插入。

    再多线程和数据量超多,大概有2000条的时候,经常闪退,全读取一次要好几秒,最后换了FMDB自己写,对象全用 NSDictionary
    其实也不是什么复杂的操作,当时大概是本机有2000条聊天记录,然后读取网络的大概几百条,要匹配本地的数据库,如果没有就添加进去,所以要循环查询好几百次,SQLitePersistentObject这货居然要7秒多,这个线程在用数据库的时候,其他线程一访问就闪退,被坑死了。
    yellowV2ex
        3
    yellowV2ex  
       2014-09-13 20:44:33 +08:00
    @yellowV2ex 后来改成先把ID都列出来,在数组里匹配了再把新的插进去,只查询一次数据库的全部ID列出来而已,居然要5秒,是SQLitePersistentObject转换对象花太多时间了
    Hysteria
        4
    Hysteria  
       2014-09-14 02:58:49 +08:00
    我用Mantle
    expkzb
        5
    expkzb  
       2014-09-14 16:53:22 +08:00
    我用 DCKeyValueMapping + ObjectiveRecord
    还算比较优雅
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   954 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:18 · PVG 04:18 · LAX 13:18 · JFK 16:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.