gpt4 book ai didi

iOS App 架构如何实现 MVVM、网络和蓝牙?

转载 作者:行者123 更新时间:2023-11-29 00:57:23 24 4
gpt4 key购买 nike

问题:

我目前在 Swift 中开发 iOS 移动应用程序时遇到问题利用:

  • BTLE :连接到外围设备并向其发送/接收数据。
  • Networking :如果外设已连接到网络(无线和/或以太网),则通过 BTLE 进行通信“可以”改为通过网络发生。
  • Model-View-ViewModel建筑
  • > RxSwift

关于应用程序:

它以蓝牙设置 View 开始,引导用户完成与外围设备配对的过程(与 TabBarController 不相交)。

与设备成功配对后,iOS 应用程序会向设备请求所有配置,该配置将作为 JSON 发送。 .

JSON包含不同的 Model应用程序向用户显示用于操作的信息(编程),需要以某种方式存储在Singleton中的数组中。庄园去哪儿view-model可以请求任何索引来显示给用户。

收到所有数据后,Bluetooth View 关闭并显示 TabBarView

当前示例:

将此应用程序关联到的一个很好的例子是 Apple Watch以及相关的 iOS 应用程序,可让您配置一切。我必须做一些相同的概念。

来自 blog post 的另一个很好的示例应用程序他们正在做的事情与我正在努力实现的目标相似。不过,我遇到的不同之处在于它们对 MVVM 的依赖注入(inject)设置(以及其他类似示例)。我使用了 Storyboard,因为他们在AppDelegate中以编程方式实例化了他们的 View Controller 。 .

我的问题...

如何在没有 NSNotifications 的情况下将数据(有效地)从 BluetoothView 传递到 TabBarViewPrepareForSegues ?请记住,我打算使用图书馆 RxSwift用于异步事件处理和事件/数据流。我试图让这个应用程序尽可能无状态。

Servers在这个blog post检索 view-models 的好习惯和/或更新它们?

最佳答案

我发现,当使用 RxSwift 时,“ View 模型”最终成为一个单一的纯函数,它从输入 UI 参数中获取可观察参数并返回可观察值,然后将这些可观察值绑定(bind)到输出 UI 元素。

真正帮助我了解 Rx 的是 tutorial videos for cycle.js .

至于您的具体难题...

您所做的不一定是“向前”运动。这样看... TabBarView 需要一些数据,它不关心这些数据来自哪里。因此,让 TabBarView 访问一个函数,该函数返回一个包含必要数据的可观察对象。该闭包将显示蓝牙 View 、建立连接、获取必要的数据,然后关闭蓝牙 View 并使用所需数据调用 onNext

查看this gist可能有助于理解我在说什么。授予 gist 使用 PromiseKit 而不是 RxSwift,但可以使用相同的原则(而不是 fulfill,你会想调用 onNext 然后 onCompletion.) 在要点中,需要数据的 View Controller 简单地调用一个函数并订阅结果(在这种情况下,结果包含一个 UIImage。)确定可用的图像源是函数的工作,询问用户希望从哪个来源检索图像并显示适当的 View Controller 以获取图像。

要点的当前内容如下:

//
// UIViewController+GetImage.swift
//
// Created by Daniel Tartaglia on 4/25/16.
// Copyright © 2016 MIT License
//

import UIKit
import PromiseKit


enum ImagePickerError: ErrorType {
case UserCanceled
}

extension UIViewController {

func getImage(focusView view: UIView) -> Promise<UIImage> {
let proxy = ImagePickerProxy()
let cameraAction: UIAlertAction? = !UIImagePickerController.isSourceTypeAvailable(.Camera) ? nil : UIAlertAction(title: "Camera", style: .Default) { _ in
let controller = UIImagePickerController()
controller.delegate = proxy
controller.allowsEditing = true
controller.sourceType = .Camera
self.presentViewController(controller, animated: true, completion: nil)
}
let photobinAction: UIAlertAction? = !UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) ? nil : UIAlertAction(title: "Photos", style: .Default) { _ in
let controller = UIImagePickerController()
controller.delegate = proxy
controller.allowsEditing = false
controller.sourceType = .PhotoLibrary
self.presentViewController(controller, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
if let cameraAction = cameraAction {
alert.addAction(cameraAction)
}
if let photobinAction = photobinAction {
alert.addAction(photobinAction)
}
alert.addAction(cancelAction)
let popoverPresentationController = alert.popoverPresentationController
popoverPresentationController?.sourceView = view
popoverPresentationController?.sourceRect = view.bounds
presentViewController(alert, animated: true, completion: nil)
let promise = proxy.promise
return promise.always {
self.dismissViewControllerAnimated(true, completion: nil)
proxy.retainCycle = nil
}
}
}


private final class ImagePickerProxy: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

let (promise, fulfill, reject) = Promise<UIImage>.pendingPromise()
var retainCycle: ImagePickerProxy?

required override init() {
super.init()
retainCycle = self
}

@objc func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = (info[UIImagePickerControllerEditedImage] as? UIImage) ?? (info[UIImagePickerControllerOriginalImage] as! UIImage)
fulfill(image)
}

@objc func imagePickerControllerDidCancel(picker: UIImagePickerController) {
reject(ImagePickerError.UserCanceled)
}
}

关于iOS App 架构如何实现 MVVM、网络和蓝牙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com