我爲一種新的,以迅速和IOS,我可能有一個同步的問題(不知道),我有一個火力點數據庫給我鑰匙,還有的在Amazon S3上傳內容的網址。我試圖從S3下載圖像,將它們存儲在數組中,然後填充tableview。SWIFT aws S3異步下載?下載圖像和填充的tableview
代碼似乎啓動下載,但它不進步(上進步一個破發點沒有按噸甚至命中)
可能有人給我如何幹淨地填充我的tableview手?我的圖像數組始終是空的,我不知道它是否來自我的s3函數(但不會返回任何錯誤),或者它來自調度事件。
//
// MainWitness.swift
// trainning2
//
// Created by Yann MASSARD on 2/2/16.
// Copyright © 2016 Witness. All rights reserved.
//
import UIKit
import Firebase
import FirebaseUI
class MainWitness: UIViewController, UITableViewDataSource, UITableViewDelegate {
let rootref = Firebase(url: "XXXXXXX/witness/")
let refpost = Firebase(url: "XXXXXXX/witness/POSTS")
var posts = [getitems]()
var Images = [UIImage]()
@IBOutlet weak var tableView: UITableView!
// S3 download function/called on view did appear, gets the key from firebase
func downloadImage(key:String){
var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
let S3BucketName: String = "witnesstest/" + rootref.authData.uid
let S3DownloadKeyName: String = key
let expression = AWSS3TransferUtilityDownloadExpression()
expression.downloadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in
dispatch_async(dispatch_get_main_queue(), {
let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend)
print("Progress is: \(progress)")
})
}
completionHandler = { (task, location, data, error) -> Void in
dispatch_async(dispatch_get_main_queue(), {
if ((error) != nil){
print("Failed with error")
print("Error: \(error!)")
}
else{
//Set your image
self.Images.append(UIImage(data: data!)!)
}
})
}
let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()
transferUtility.downloadToURL(nil, bucket: S3BucketName, key: S3DownloadKeyName, expression: expression, completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let exception = task.exception {
print("Exception: \(exception.description)")
}
if let _ = task.result {
print("Download Starting!") // shows in console for each firebase post loop
}
return nil;
}
}
override func viewDidAppear(animated: Bool) {
// [1] Call the queryOrderedByChild function to return a reference that queries by the "author" property
self.refpost.queryOrderedByChild("author").observeEventType(.Value, withBlock: { snapshot in
var newItems = [getitems]()
for item in snapshot.children {
let postitem = getitems(snapshot: item as! FDataSnapshot)
newItems.append(postitem)
// READ IMAGE FROM S3
self.downloadImage(postitem.imagekey)
}
self.posts = newItems
self.tableView.reloadData()
})
}
override func viewDidLoad() {
//ref.unauth()
super.viewDidLoad()
//tableview datasource and delegate
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
// is any user log ?
if ((rootref.authData) != nil) {
print("user logged")
} else {
print("user not logged")
dispatch_async(dispatch_get_main_queue()){
self.performSegueWithIdentifier("log", sender: self)
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: MainWitnessTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("RIcell") as! MainWitnessTableViewCell
// populate the cell
let postitem = posts[indexPath.row]
cell.postOwner.text = postitem.author
cell.postContent.text = postitem.content
cell.postDate.text = postitem.createon
// cell.cellImage.image = Images[indexPath.row] // returns array index out of range
return cell
}
func tableView(tableView: UITableView, didselectRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
}