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.