2
私はブロック・モーメントを持っています。 getDataByDate
のcase .success
にデータがあります。それは配列です。その後、その配列をcallAnother
にプッシュし、ループ内の配列の要素を処理する必要があります。すべての要素は、私がmyAnotherMethod
に押し込み、completion
ブロックに私がarrayForDataSourceSave
を作成したいのですが、私のadd
が空であるself?.dataSource.save(data: data, add: arrayForDataSourceSave
あるクロージャから別のクロージャへ実際のデータを取得する方法は?
毎回にそれを送りました。これを修正するには?
private func callAnother(data: [AnyModel], completion: @escaping() -> Void) {
var arrayForDataSourceSave: []()
for element in data {
guard let id = element.id else { return }
APIService.myAnotherMethod(id: id, completion: { result in
switch result {
case .success(let well):
arrayForDataSourceSave.append(well)
print(well)
case .error(let error):
print("request error: \(error)")
}
})
}
completion()
}
func refresh(completion: @escaping() -> Void) {
APIService.getDataByDate(date: date, completion: { [weak self] (result) in
switch result {
case .success(let data):
self?.callAnother(data: data, completion: {
self?.dataSource.save(data: data, add: arrayForDataSourceSave)
})
case .error(let error):
print("request error: \(error)")
}
completion()
})
}
あなたは機能をリフレッシュするためにarrayForDataSourceSaveを渡そうとしていますか?私は完了ブロックで配列を渡すことができると思います: "プライベート関数callAnother(データ:[AnyModel]、完了:@エスケープ(結果:[YourArray]) - >())"。成功の呼び出しを完了します。 –
コードがコンパイルをパスしていますか? 'var arrayForDataSourceSave:[]()'構文が間違っているコンストラクタを間違った場所で呼び出すと、構文が正しくありません。var arrayForDataSourceSave:[Any] = [] 'または' var arrayForDataSourceSave = [Any]() ' 'callAnother(data:completion:)'関数のローカル変数であれば 'refresh(completion:)'関数から 'arrayForDataSourceSave'にアクセスしていますか? –