iosiphonecore-dataswift2xcode7-beta6

Passing Core Data Record from UITextView to UIWebView


I am having an issue passing a core data record between a UITextView and a UIWebView. The idea behind this is to load user inputted HTML code into the web view to be previewed. I am having one major problem and that is; My user passed HTML will load on the second pass (or second click of the action button). Essentially, the UIWebView is displaying the previous revision and not the most current revision of the HTML boilerplate code. What is confusing me even further is that on the return from the UIWebView I have code set up to delete the record so it (the record) shouldn't even exist on a second pass.

Here is the code I have on my UITextView

    @IBAction func previewCompile(sender: AnyObject) {
    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as!  AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext
    let userCode = NSEntityDescription.insertNewObjectForEntityForName("Data", inManagedObjectContext: context)
    let request = NSFetchRequest(entityName: "Data")
    request.returnsObjectsAsFaults = false

    do {
        userCode.setValue(codeView.text, forKey: "code")
        htmlPreviewCode = (userCode.valueForKey("code") as? String)!
        try context.save()

        print(htmlPreviewCode)
    } catch {
        print("No record was saved!")
    }

}

Here is the code on the receiving UIWebView

var html = String()

    @IBOutlet weak var deleteRecord: UIButton!

    @IBAction func goBACK(sender: AnyObject) {

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext
    let request = NSFetchRequest(entityName: "Data")
    request.returnsObjectsAsFaults = false

    do {
        let results = try context.executeFetchRequest(request)
        if results.count > 0 {
            for result: AnyObject in results {
                context.deleteObject(result as! NSManagedObject)
                print("Record Deleted")
            }
        }

    } catch {
        print("Record failed to delete!")
    }

}

    @IBOutlet weak var webview: UIWebView!

    override func viewDidLoad() {
         super.viewDidLoad()
              html = htmlPreviewCode
              webview.loadHTMLString(html, baseURL: nil)
}

Any help is truly appreciated! This is my first app build and first time learning a programming language. So, please forgive me if I'm missing any major details.


Solution

  • Your viewDidLoad function will only be called one time in the lifecycle of the view controller. So, any subsequent changes you make to your html string will happen AFTER viewDidLoad has happened.

    Experiment with moving your code to viewDidAppear if you have multiple controllers, or better, put it in its own func and call that func.

    example

     func updateUI() {
              html = htmlPreviewCode
              webview.loadHTMLString(html, baseURL: nil)
           }
    

    if you wanted to get really fancy you could do this in your didSet method of the htmlPreviewCode

     var htmlPreviewCode : String {
            didSet {
                    webview.loadHTMLString(htmlPreviewCode, baseURL: nil)
             }
           }
    

    Now, every time the htmlPreviewCode changes, it will update the webView and you don't need any updateUI function.

    --Please Note-- I wrote the above code without testing it and I haven't had enough coffee yet this morning. So, the idea is sound, but the syntax might need a little tweaking.