Reputation: 145
I am trying to upload files into AWS S3
. Files I am selecting from iCloud
and appending into array
for listing in tableView
and same time calling AWS S3 upload
function.
Here, once file uploaded successfully I need to make some identification inside array
because whenever I am trying to upload next file
, my array again pushing already stored
files. So, The upload process giving duplication
results. How to avoid this problem?
My complication if i remove uploaded file url
from array then I can't able to list in tableView
but If I didn't remove, Then it will be re-uploading
whenever I am trying to upload new file.
// Array declaration
var items = [Item]()
var fileArray = [Item]()
// Values appending into my array
items.append(Item(indexPath: indexPath, file: item.url))
// TableView data-load
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
let item = fileArray[indexPath.row]
if fileArray.count > 0 {
cell.url_label.text = item.url
// Upload function call
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.uploadData(indexPath: indexPath, file: item.url)
})
}
return cell
}
// Upload function with parameters
private func uploadData(indexPath: indexPath, file: item.url) {
// Tracking each cell and it's file.
// Once, file uploaded I am doing some changes in tableview cell
}
Upvotes: 1
Views: 109
Reputation: 100503
1- You shouldn't do any non-ui tasks inside
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
2- You shouldn't upload files inside cellForRowAt
as it's called every scroll so for sure you'll encounter a duplication , if you want to only do this for visible cells , then inside the array model create a status property of type Enum
to indicate the current state of this file -> NotUploaded-Uploaded-Uploading and check it before upload
Enum Status {
case notUploaded,uploaded,uploading
}
class Item {
var st:Status = .notUploaded
func upload () {
if st == .notUploaded {
st =.uploading
// upload here
API.upload {
self.st = .uploaded
}
}
}
}
Upvotes: 1