最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift 本地数据保存

iOS crifan 3060浏览 0评论

swift 本地数据保存

iOS开发——数据持久化&本地数据的存储(使用NSCoder将对象保存到.plist文件) – iCocos – 博客园

Swift – 本地数据的保存与加载(使用NSCoder将对象保存到.plist文件)

Swift之沙盒与数据存储

ios查看CoreData数据保存SQLite文件路径(swift版本)

swift local storage

swift SQlite3 coredata

Core Data: How to Preload Data and Use Existing SQLite Database

和 Jesse Squires 一起在 Swift 中使用 Core Data – Realm is a mobile database: a replacement for SQLite & Core Data

swift store data locally

swift core data library

Swift core data: how to store objects

pepibumur/SugarRecord: Data persistence management library written in Swift 2.0

JohnEstropia/CoreStore: Unleashing the real power of Core Data with the elegance and safety of Swift

Getting Started with Core Data Tutorial

swift SugarRecord 教程

Swift语言指南 – GCZtian的专栏 – 博客频道 – CSDN.NET

Realm is a mobile database: a replacement for SQLite & Core Data

Swift Docs – Realm is a mobile database: a replacement for SQLite & Core Data

GitHub 上面有哪些适合新手阅读和学习的关于 iOS 开发的开源项目? – 知乎

-》目前看起来,还是:

Swift Docs – Realm is a mobile database: a replacement for SQLite & Core Data

好用。

抽空去试试。

【后记】

后来别人写了realm相关代码,如下:

<code>import RealmSwift


func getProvinceList(_ parentId:Int = RegionAllProvinceParentId){
    // 获取默认的 Realm 数据库
    let realm = try! Realm()
    let ducumentPath2 = NSHomeDirectory() + "/Documents"
    
    let url = URL(string: ducumentPath2)
    //try realm.writeCopyToURL(NSURL(string: ducumentPath2)!)
    let fileURL = url!.deletingLastPathComponent()
        .appendingPathComponent("default.realm")
    do {
        try realm.writeCopy(toFile: (fileURL as NSURL) as URL)
    } catch {
        //XCTFail("writeCopyToURL failed")
    }

    // 检索 Realm 数据库
   let puppies = realm.objects(RegionItems).filter("pid == 1")
   
   if  puppies.count != 31 {
       getUrlRespDataJson_async(
           .GET,
           url: ServerApi.getRegionList(gCurUserItem.id, parentId: parentId),
           respJsonHandler: getProvinceListHandlers)
   } else {
       for item in puppies {
           getCityList(item.id)
       }
   
   }
}


func getProvinceListHandlers(_ respDataJson:Alamofire.Result&lt;JSON&gt;, mergedAllPara:Dictionary&lt;String, AnyObject&gt;) {
    gLog.verbose("respDataJson.debugDescription=\(respDataJson.debugDescription)")
    switch respDataJson {
    case .success(let dataJson):
        print(dataJson)
        
        guard let dataJsonArr = dataJson.array else {
            gLog.error("get empty region province list")
            return
        }
        let realm = try! Realm()
        for eachRegionJson in dataJsonArr {
            gLog.verbose("eachRegionJson=\(eachRegionJson)")
            
            let curRegionItem = parseJsonToRegionItem(eachRegionJson)
            gLog.verbose("curRegionItem=\(curRegionItem)")
            let  regionItems = RegionItems()
            regionItems.id = curRegionItem.id
            regionItems.code = "\(curRegionItem.code)"
            regionItems.name = curRegionItem.name
            regionItems.pid  = curRegionItem.parentId

            // 检索 Realm 数据库
            let puppies = realm.objects(RegionItems.self).filter("id == \(regionItems.id)")
            if puppies.count == 0 {
                try! realm.write {
                    realm.add(regionItems)
                }
            }
        }
        
        let ducumentPath2 = NSHomeDirectory() + "/Documents"
        
        let url = URL(string: ducumentPath2)
        //try realm.writeCopyToURL(NSURL(string: ducumentPath2)!)
        let fileURL = url!.deletingLastPathComponent()
            .appendingPathComponent("default.realm")
        do {
            try realm.writeCopy(toFile: (fileURL as NSURL) as URL)
        } catch {
            //XCTFail("writeCopyToURL failed")
        }
        
    case .failure(let error):
        gLog.verbose("error=\(error)")
    }

}

</code>

供参考。

转载请注明:在路上 » 【已解决】swift 本地数据保存

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.215 seconds, using 22.13MB memory