我有一个随视图一起加载的文本视图。然后用户可以编辑和修改该值,当用户在导航按钮中按回去时,将启动 viewWillDisappear((,并将 textView 出口的值传递给交互器,交互器应从 dataManager 触发一个方法将该值保存到核心数据中。
我的问题是在不违反毒蛇规则的情况下传递此参数的最佳方法是什么?
我这样做了,但我几乎可以肯定我做错了。这是我第一次体验VIPER建筑,任何帮助将不胜感激。
查看文件:
import Foundation
import UIKit
class NoteDetailView : UIViewController {
var presenter: NoteDetailPresenterProtocol?
@IBOutlet weak var detailNoteText: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
presenter?.viewDidLoad()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
presenter?.updatedText = detailNoteText.text
presenter?.viewWillDisappear()
}
}
extension NoteDetailView : NoteDetailViewProtocol {
func showNote(noteDetail note: NoteModel) {
detailNoteText.text = note.noteText
}
}
主持人:
import Foundation
class NoteDetailPresenter : NoteDetailPresenterProtocol {
var view: NoteDetailViewProtocol?
var updatedText: String?
var note: NoteModel?
var router: NoteDetailRouterProtocol?
var interactor: NoteDetailInteractorProtocol?
func viewDidLoad(){
view?.showNote(noteDetail: note!)
}
func viewWillDisappear() {
guard note?.noteText.isEmpty == false, let text = updatedText else {return}
interactor?.retrieveNote(note: note!, updatedText : text)
}
}
交互者:
import Foundation
class NoteDetailInteractor : NoteDetailInteractorProtocol {
var dataManager: NewNoteDataManagerProtocol?
var localDataManager: NoteListLocalDataManagerInputProtocol?
func retrieveNote(note: NoteModel, updatedText : String) {
do {
if let noteList = try localDataManager?.retrieveNoteList() {
let noteModelList = noteList.map(){
return NoteModel(noteText : $0.noteText != nil ? $0.noteText! : "", creationDate : $0.creationDate != nil ? $0.creationDate! : "")
}
if !noteModelList.isEmpty {
var noteTextExist = false
var indexPath = 0
for (index,newnote) in noteModelList.enumerated(){
if newnote == note {
noteTextExist = true
indexPath = index
}
}
if noteTextExist == true {
do {
noteList[indexPath].noteText = updatedText
try dataManager?.updateNote(note: noteList[indexPath])
}
catch {
print(error)
}
}
}
}
}
catch {
print(error)
}
}
}
数据管理器:
import Foundation
import CoreData
import UIKit
class NewNoteDataManager : NewNoteDataManagerProtocol{
func deleteNote(note: Note) throws {
guard let managedOC = CoreDataStore.managedObjectContext else {
throw PersistenceError.managedObjectContextNotFound
}
managedOC.delete(note)
do {
try managedOC.save()
}
catch let error as NSError {
print(error)
}
}
func updateNote(note: Note) throws {
guard let managedOC = CoreDataStore.managedObjectContext else {
throw PersistenceError.managedObjectContextNotFound
}
do {
try managedOC.save()
}
catch let error as NSError {
print(error)
}
}
func saveNote(noteText: String) throws {
guard let managedOC = CoreDataStore.managedObjectContext else {
throw PersistenceError.managedObjectContextNotFound
}
if let entity = NSEntityDescription.entity(forEntityName: String("Note"), in: managedOC) {
do {
let newNote = Note(entity: entity, insertInto: managedOC)
newNote.noteText = noteText
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
newNote.creationDate = formatter.string(from: date)
try managedOC.save()
}catch let error as NSError {
print(error)
}
}else {
throw PersistenceError.couldNotSaveObject
}
}
}
完整的项目可以在这里找到: https://github.com/AlfroMlg/Notes
老实说,我认为你的方法还可以。有人可能会争辩说,最好不要在演示器中存储"updateText"和"note"之类的数据,而是使用方法通过演示器传递这些数据。
您应该在演示者弱中引用视图,以避免保留周期。 由于当演示者处于活动状态时,交互器和路由器都不会为零,因此您可以将它们声明为非可选并在 init 中初始化它们。交互器中的 dataManager 引用也是如此。