簡體   English   中英

將JSON保存為String作為String並使用String創建對象數組

[英]Save json to CoreData as String and use the String to create array of objects

我正在為一個廣播電台創建一個應用程序,我想將“顯示”對象存儲到一個數組中。 我使用網絡服務器提供json數據來填充數組,但是我想將此json數據作為字符串存儲到CoreData中,以便對數組的訪問不依賴於互聯網連接。 因此,我想在應用啟動時更新CoreData中的字符串,但要基於存儲在CoreData中的字符串而不是基於Web服務器上的json數據創建一個數組。

這是我從網絡服務器下載json數據並將其存儲到字符串中的功能:

func downloadShows() {
    let urlPath = "http://dogradioappdatabase.com/shows.php"

    guard let url = URL(string: urlPath) else {return}

    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard let dataResponse = data,
            error == nil else {
                print(error?.localizedDescription ?? "Response Error")
                return }

        let jsonAsString = self.jsonToString(json: dataResponse)

        DispatchQueue.main.async {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

            let task2 = WebServer(context: context) // Link Task & Context
            task2.showsArray = jsonAsString
            print(jsonAsString)
            (UIApplication.shared.delegate as! AppDelegate).saveContext()
        }
    }
    task.resume()

}    

func jsonToString(json: Data) -> String {
    let convertedString: String

        convertedString = String(data: json, encoding: String.Encoding.utf8)! // the data will be converted to the string
    return convertedString
}

這是從CoreData提取的json創建shows數組的函數:

    func createShowsArray () -> Array<ShowModel> {
    var array: Array<ShowModel> = Array()
    var tasks: [WebServer] = []

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    do {
        tasks = try context.fetch(WebServer.fetchRequest())
    }
    catch {
        print("Fetching Failed")
    }

    let arrayAsString: String = tasks[0].showsArray!
    print(arrayAsString)
    do {
        let data1 = arrayAsString.data(using: .utf8)!
        let decoder = JSONDecoder()
        array = try decoder.decode([ShowModel].self, from:
            data1)

    } catch let parsingError {
        print("Error", parsingError)
    }

    return array
}

但是,這不能正確地將數據加載到數組中。 我在downloadShows()函數(jsonAsString)中打印了保存到CoreData的值,並將其作為響應:

[{“名稱”:“示例節目2”,“ ID”:“ 2”,“描述”:“此...

但是,當我在createShowsArray()函數(arrayAsString)中從CoreData獲取字符串時,它添加了“ DOG_Radio.ShowModel”

[DOG_Radio.ShowModel(Name:“ Example Show 2”,ID:“ 2”,說明:“ This ...

JSON解碼器不會將arrayAsString解碼為實際的數組。 它把這個扔回來:

錯誤dataCorrupted(Swift.DecodingError.Context(codingPath:[],debugDescription:“給定的數據不是有效的JSON。”,底層錯誤:可選(錯誤域= NSCocoaErrorDomain代碼= 3840,字符1周圍的值無效)。UserInfo= {NSDebugDescription =字符1周圍的值無效。})))

抱歉,這個問題很長,我只是不知道如何使用CoreData將json保存為String,然后將該String轉換為數組

將json數據或“整個原始數據”存儲到CoreData中是一種不良做法。 而是將Show本身存儲為NSManagedObject 您可以通過將JSON數據轉換為對象(看起來就像您已經在做的那樣),然后從它們創建CoreData NSManagedObjects來實現。

實際上,如果您可以輕松地從JSON轉換數據,則在保存到CoreData之前無需將其轉換為字符串。 您可以簡單地將Data存儲為NSData ,即可transformablebinary data ,如果無法成功獲取到服務器,則可以稍后將其重新轉換。

但是,從長遠來看,這並不是那么可靠,並且工作起來更加困難。 數據可能已損壞和/或格式錯誤。

簡而言之,您需要一個數據模型和一個JSON可讀數據結構,您可以將其轉換為數據模型以供CoreData管理。 當您希望允許用戶更新,刪除,保存或過濾單個Show時,這將變得很重要。

Codable允許您使用JSONDecoder().decode(_:from:)從JSON轉換為Struct。

ShowModelCodeable.swift

import Foundation

struct ShowModelCodeable: Codable {
    var name: String?
    var description: String?
    var producer: String?
    var thumb: String?
    var live: String?
    var banner: String?
    var id: String?

    enum CodingKeys: String, CodingKey {
        case name = "Name"
        case id = "ID"
        case description = "Description"
        case producer = "Producer"
        case thumb = "Thumb"
        case live = "Live"
        case banner = "Banner"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        name = try values.decode(String.self, forKey: .name)
        description = try values.decode(String.self, forKey: .description)
        producer = try values.decode(String.self, forKey: .producer)
        thumb = try values.decode(String.self, forKey: .thumb)
        live = try values.decode(String.self, forKey: .live)
        banner = try values.decode(String.self, forKey: .banner)
        id = try values.decode(String.self, forKey: .id)

    }

    func encode(to encoder: Encoder) throws {

    }
}

接下來,我們需要一個核心數據棧和一個CoreData實體。 將Core Data Stack創建為Class Singleton可以在應用程序中的任何位置進行訪問都是很常見的。 我在基本操作中包括了一個:

核心數據模型的圖像

DatabaseController.Swift

import Foundation
import CoreData

class DatabaseController {

    private init() {}

    //Returns the current Persistent Container for CoreData
    class func getContext () -> NSManagedObjectContext {
        return DatabaseController.persistentContainer.viewContext
    }


    static var persistentContainer: NSPersistentContainer = {
        //The container that holds both data model entities
        let container = NSPersistentContainer(name: "StackOverflow")

        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */

                //TODO: - Add Error Handling for Core Data

                fatalError("Unresolved error \(error), \(error.userInfo)")
            }


        })
        return container
    }()

    // MARK: - Core Data Saving support
    class func saveContext() {
        let context = self.getContext()
        if context.hasChanges {
            do {
                try context.save()
                print("Data Saved to Context")
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate.
                //You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }

    /* Support for GRUD Operations */

    // GET / Fetch / Requests
    class func getAllShows() -> Array<ShowModel> {
        let all = NSFetchRequest<ShowModel>(entityName: "ShowModel")
        var allShows = [ShowModel]()

        do {
            let fetched = try DatabaseController.getContext().fetch(all)
            allShows = fetched
        } catch {
            let nserror = error as NSError
            //TODO: Handle Error
            print(nserror.description)
        }

        return allShows
    }

    // Get Show by uuid
    class func getShowWith(uuid: String) -> ShowModel? {
        let requested = NSFetchRequest<ShowModel>(entityName: "ShowModel")
        requested.predicate = NSPredicate(format: "uuid == %@", uuid)

        do {
            let fetched = try DatabaseController.getContext().fetch(requested)

            //fetched is an array we need to convert it to a single object
            if (fetched.count > 1) {
                //TODO: handle duplicate records
            } else {
                return fetched.first //only use the first object..
            }
        } catch {
            let nserror = error as NSError
            //TODO: Handle error
            print(nserror.description)
        }

        return nil
    }

    // REMOVE / Delete
    class func deleteShow(with uuid: String) -> Bool {
        let success: Bool = true

        let requested = NSFetchRequest<ShowModel>(entityName: "ShowModel")
        requested.predicate = NSPredicate(format: "uuid == %@", uuid)


        do {
            let fetched = try DatabaseController.getContext().fetch(requested)
            for show in fetched {
                DatabaseController.getContext().delete(show)
            }
            return success
        } catch {
            let nserror = error as NSError
            //TODO: Handle Error
            print(nserror.description)
        }

        return !success
    }

}

// Delete ALL SHOWS From CoreData
class func deleteAllShows() {
    do {
        let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "ShowModel")
        let deleteALL = NSBatchDeleteRequest(fetchRequest: deleteFetch)

        try DatabaseController.getContext().execute(deleteALL)
        DatabaseController.saveContext()
    } catch {
        print ("There is an error in deleting records")
    }
}

最后,我們需要一種獲取JSON數據並將其轉換為對象,然后顯示的方法。 請注意,當按下更新按鈕時,它將觸發getDataFromServer() 這里最重要的一行是

self.newShows = try JSONDecoder().decode([ShowModelCodeable].self, from: dataResponse)

正在從您的服務器下拉顯示,並將其轉換為ShowModelCodeable對象。 一旦newShows設置將在運行代碼didSet ,在這里你可以刪除在上下文中的所有對象,然后運行addNewShowsToCoreData(_:)創建要保存的背景下,新NSManagedObjects。

我創建了一個基本的視圖控制器,並以編程方式添加了tableView來管理數據。 在這里, Shows是來自CoreData的NSManagedObject數組,而newShows是從我們從服務器請求中獲取的json編碼的新對象。

ViewController.swift

import Foundation
import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // Properties
    var Shows:[ShowModel]?

    var newShows:[ShowModelCodeable]? {
        didSet {
            // Remove all Previous Records
            DatabaseController.deleteAllShows()
            // Add the new spots to Core Data Context
            self.addNewShowsToCoreData(self.newShows!)
            // Save them to Core Data
            DatabaseController.saveContext()
            // Reload the tableView
            self.reloadTableView()
        }
    }

    // Views
    var tableView: UITableView = {
        let v = UITableView()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    lazy var updateButton: UIButton = {
        let b = UIButton()
        b.translatesAutoresizingMaskIntoConstraints = false
        b.setTitle("Update", for: .normal)
        b.setTitleColor(.black, for: .normal)
        b.isEnabled = true
        b.addTarget(self, action: #selector(getDataFromServer), for: .touchUpInside)
        return b
    }()

    override func viewWillAppear(_ animated: Bool) {
        self.Shows = DatabaseController.getAllShows()
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(ShowCell.self, forCellReuseIdentifier: ShowCell.identifier)

        self.layoutSubViews()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    //TableView -
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return DatabaseController.getAllShows().count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // 100
        return ShowCell.height()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: ShowCell.identifier) as! ShowCell

        self.Shows = DatabaseController.getAllShows()

        if Shows?.count != 0 {
            if let name = Shows?[indexPath.row].name {
                cell.nameLabel.text = name
            }

            if let descriptionInfo = Shows?[indexPath.row].info {
                cell.descriptionLabel.text = descriptionInfo
            }
        } else {
            print("No shows bros")
        }


        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // Show the contents
        print(Shows?[indexPath.row] ?? "No Data For this Row.")

    }

    func reloadTableView() {
        DispatchQueue.main.async {
         self.tableView.reloadData()
        }
    }


    func layoutSubViews() {
        let guide = self.view.safeAreaLayoutGuide
        let spacing: CGFloat = 8

        self.view.addSubview(tableView)
        self.view.addSubview(updateButton)

        updateButton.topAnchor.constraint(equalTo: guide.topAnchor, constant: spacing).isActive = true
        updateButton.leftAnchor.constraint(equalTo: guide.leftAnchor, constant: spacing * 4).isActive = true
        updateButton.rightAnchor.constraint(equalTo: guide.rightAnchor, constant: spacing * -4).isActive = true
        updateButton.heightAnchor.constraint(equalToConstant: 55.0).isActive = true

        tableView.topAnchor.constraint(equalTo: updateButton.bottomAnchor, constant: spacing).isActive = true
        tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: guide.bottomAnchor, constant: spacing).isActive = true
    }


    @objc func getDataFromServer() {
        print("Updating...")
        let urlPath = "http://dogradioappdatabase.com/shows.php"

        guard let url = URL(string: urlPath) else {return}

        let task = URLSession.shared.dataTask(with: url) {
            (data, response, error) in
                guard let dataResponse = data, error == nil else {
                        print(error?.localizedDescription ?? "Response Error")
                return }

            do {
                self.newShows = try JSONDecoder().decode([ShowModelCodeable].self, from: dataResponse)
            } catch {
                print(error)
            }

        }

        task.resume()
    }




    func addNewShowsToCoreData(_ shows: [ShowModelCodeable]) {

        for show in shows {
            let entity = NSEntityDescription.entity(forEntityName: "ShowModel", in: DatabaseController.getContext())
            let newShow = NSManagedObject(entity: entity!, insertInto: DatabaseController.getContext())

            // Create a unique ID for the Show.
            let uuid = UUID()
            // Set the data to the entity
            newShow.setValue(show.name, forKey: "name")
            newShow.setValue(show.description, forKey: "info")
            newShow.setValue(show.producer, forKey: "producer")
            newShow.setValue(show.thumb, forKey: "thumb")
            newShow.setValue(show.live, forKey: "live")
            newShow.setValue(show.banner, forKey: "banner")
            newShow.setValue(show.id, forKey: "id")
            newShow.setValue(uuid.uuidString, forKey: "uuid")
        }

    }



}

查看層次結構

試試這個對我有用

// Convert JSON to String
func jsonToString(json: AnyObject)->String{
    do {
        let data1 =  try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data
        let convertedString = String(data: data1, encoding: String.Encoding.utf8) // the data will be converted to the string
        return convertedString! // <-- here is ur string

    } catch let myJSONError {
        print(myJSONError)
    }

    return ""
}

// Convert JSON String to Dict
func convertToDictionary(text: String) -> NSDictionary!{
    if let data = text.data(using: .utf8) {
        do {

            return try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
        } catch {
            print(error.localizedDescription)
        }
    }
    return nil
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM