我有一個小的阿波羅iOS應用,我在表視圖中顯示的會議列表,並希望能夠在下面的設置到會議添加到列表:自動UI更新
GraphQL:
query AllConferences {
allConferences {
...ConferenceDetails
}
}
mutation CreateConference($name: String!, $city: String!, $year: String!) {
createConference(name: $name, city: $city, year: $year) {
...ConferenceDetails
}
}
fragment ConferenceDetails on Conference {
id
name
city
year
attendees {
...AttendeeDetails
}
}
fragment AttendeeDetails on Attendee {
id
name
conferences {
id
}
}
ConferencesTableViewController:
class ConferencesTableViewController: UITableViewController {
var allConferencesWatcher: GraphQLQueryWatcher<AllConferencesQuery>?
var conferences: [ConferenceDetails] = [] {
didSet {
tableView.reloadData()
}
}
deinit {
allConferencesWatcher?.cancel()
}
override func viewDidLoad() {
super.viewDidLoad()
allConferencesWatcher = apollo.watch(query: AllConferencesQuery()) { result, error in
print("Updating conferences: ", result?.data?.allConferences)
guard let conferences = result?.data?.allConferences else {
return
}
self.conferences = conferences.map { $0.fragments.conferenceDetails }
}
}
// ...
// standard implementation of UITableViewDelegate
// ...
}
AddConferenceViewController:
class AddConferenceViewController: UIViewController {
// ... IBOutlets
@IBAction func saveButtonPressed() {
let name = nameTextField.text!
let city = cityTextField.text!
let year = yearTextField.text!
apollo.perform(mutation: CreateConferenceMutation(name: name, city: city, year: year)) { result, error in
if let _ = result?.data?.createConference {
self.presentingViewController?.dismiss(animated: true)
}
}
}
}
我也實施cacheKeyForObject
的AppDelegate像這樣:
apollo.cacheKeyForObject = { $0["id"] }
我的問題是,是否有可能從自動UI更新與此設置中受益?當前執行CreateConferenceMutation
時,表格視圖不會更新。我缺少的東西還是我打的是在docs提到的限制:
在某些情況下,只用
cacheKeyFromObject
是不夠的,你的應用程序UI正確更新。例如,如果您想在不重新列表整個列表的情況下向對象列表中添加內容,或者有某些對象無法分配對象標識符,則Apollo無法自動爲您更新現有查詢。