【iOS】Swift3:context.executeFecthRequest(request, error: nil)

本文介绍如何在Swift3中使用Core Data框架执行数据请求。重点讲解了从context.executeFecthRequest到context.fetch(request)的变化,并强调了在调用fetch方法时需要处理异常。

context.executeFecthRequest(request, error: nil)

在Swift3中改为:

context.fecth(request)

且要处理异常,即:

try! context.fetch(request)

这些代码写了什么:// // HomeDeviceManagementViewController.swift // OmadaSurveillance // // Created by LSL on 3/12/25. // import Cocoa //import OmadaSurveillanceCore @preconcurrency import WebKit import CocoaHTTPServer class HomeDeviceManagementViewController: TPHomeBaseViewController, WKNavigationDelegate { var webView = WKWebView(frame: .zero) var blurView = NSView() var isRetried:Bool = false let appcontext = TPAppContextFactory.shared() override func setupSubviews() { super.setupSubviews() NotificationCenter.default.addObserver(self, selector: #selector(handleLogout), name: .accountLogout, object: nil) blurView.wantsLayer = true blurView.layer?.backgroundColor = NSColor.tpBackground.cgColor TPBWaitingToastView.show(in: blurView, message: nil, style: .noMaskAndToast) if #available(macOS 13.3, *) { webView.isInspectable = true } webView.navigationDelegate = self view.addSubview(webView) view.addSubview(blurView) } override func setupText() { super.setupText() reloadWebView() } override func makeConstraints() { super.makeConstraints() webView.snp.makeConstraints({ make in make.edges.equalTo(view) }) blurView.snp.makeConstraints { make in make.edges.equalTo(view) } } func reloadWebView() { let currentWebLanguage = GetCloud2Locale() if !appcontext.isLogin { let request = URLRequest(url: URL(string: "https://localhost:\(sharedPort)/index.html?tempKey=1&language=\(currentWebLanguage)&vmsId=d50e22bb203147d094bd77e84e524539&type=local#/vms/device")!) self.webView.load(request) } else if appcontext.isCloudLogin && !appcontext.isVmsLogin { let request = URLRequest(url: URL(string: "https://localhost:\(sharedPort)/index.html?tempKey=1&language=\(currentWebLanguage)&vmsId=d50e22bb203147d094bd77e84e524539&type=cloud#/vms/device")!) self.webView.load(request) } else if appcontext.isCloudVMSLogin || appcontext.isCloudAccessVMSLogin { // 如果是企业版,获取tempkey,等到获取后tempkey后刷新webView if TPSSAppContext.shared.isCloudVMSLogin || TPSSAppContext.shared.isCloudAccessVMSLogin { let callback: TPGuardDeviceCloudCallback = { [weak self] response in if let self = self, let tempkey = response?["result"] as? String { let urlString = TPPlayerEnvironmentManager().VMS_CLOUD_DOMAIN + "?orgUrl=\(appcontext.accountInfo.vmsUserUrl)&tempKey=\(tempkey)&vmsId=\(appcontext.currentVMSId)&target=device&from=pcClient&version=1.0&theme=dark&language=\(currentWebLanguage)&capability=\(capability)&port=\(sharedPort)#/vms" let request = URLRequest(url: URL(string: urlString)!) webView.load(request) } } appcontext.requestVMSTempKey(callback: callback) } } } func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { print("didCommit") blurView.isHidden = false } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { blurView.isHidden = true } // 页面加载完成,渲染失败,触发该回调 func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: any Error) { if !isRetried { webView.reload() isRetried = true } } // 页面尚未开始加载,由于url无效、网络问题、服务器处理请求超时导致加载失败,触发该回调 func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: any Error) { if !isRetried { webView.reload() isRetried = true } } // 页面传输中断,触发该回调 func webViewWebContentProcessDidTerminate(_ webView: WKWebView) { if !isRetried { webView.reload() isRetried = true } } func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { // 对需要信任的网页,拦截challenge并信任,否则webView无法加载 if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { if let serverTrust = challenge.protectionSpace.serverTrust { let credential = URLCredential(trust: serverTrust) completionHandler(.useCredential,credential) } else { completionHandler(.cancelAuthenticationChallenge,nil) } } else { completionHandler(.performDefaultHandling, nil) } } @objc func handleLogout() { if let url = URL(string: "https://index.html") { let urlRequest = URLRequest(url: url) webView.load(urlRequest) } } }
11-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值