2017-02-26 3 views
0

현재 작업중인 앱의 방문 페이지에는 사용자가 활발하게 라이브 스트리밍하지 않는 경우 애플리케이션의 이름이 지정된 장소 홀더 아바타가 있습니다 (Red5 Pro 스트리밍 사용 이에 대한 프레임 워크).라이브 스트림을 시작한 후 사용자를 새로 고치기 위해 UITableView 가져 오기

누군가 으로 시작되면 자동으로 테이블 뷰를 새로 고침하고 새 사용자의 아바타를 표시하고 싶습니다. 내가 지금까지 작성한 작품은 전부는 아니지만. 누군가가 자리 표시 자 아바타 을 살면이 사라지지만 스트리밍의 아바타 사용자는이 표시되지 않습니다.

앱을 닫았다가 다시 열면 올바르게 표시됩니다. 스위프트 3에 내 코드가 있는데, 내가 뭘 잘못하고 있니? 새로 고침 호출이 ViewDidLoad 밖으로 이동해야합니까? Dispatch Queue를 잘못 사용하고 있습니까? 감사합니다

import UIKit 
import Firebase 

class HomeController: UIViewController, UITableViewDataSource,  UITableViewDelegate, cellDelegate { 

@IBOutlet var tableView: UITableView! 
var stream: String! 
var top: [SSStream] = [] 
var recent: [SSStream] = [SSStream()] 
var trending: [SSStream] = [SSStream()] 
var ref: FIRDatabaseReference! 

override func viewDidLoad() { 
    navigationItem.title = "Swiffshot" 
    navigationController?.navigationBar.isTranslucent = false 
    let settings = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20)) 
    settings.setImage(#imageLiteral(resourceName: "Settings"), for: .normal) 
    settings.addTarget(self, action: #selector(settingsPressed), for: .touchUpInside) 
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: settings) 
    let friends = UIButton(frame: CGRect(x: 0, y: 0, width: 23, height: 20)) 
    friends.setImage(#imageLiteral(resourceName: "AllFriends"), for: .normal) 
    friends.addTarget(self, action: #selector(friendsPressed), for: .touchUpInside) 
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: friends) 

    let nib = UINib(nibName: "MainHeader", bundle: Bundle.main) 
    tableView.register(nib, forHeaderFooterViewReuseIdentifier: "MainHeader") 

    ref = FIRDatabase.database().reference() 


    if !SSContact.shared.active { 
     performSegue(withIdentifier: "fromMainToAuth", sender: self) 
    } 

    SSContact.shared.load() { SSContact.shared.propertyCheck(self) { } } 

//  SSContact.shared.subscribeToTop(pulse: { (streams) in 
//   self.top.removeAll() 
//   self.top.append(contentsOf: streams) 
//   self.tableView.reloadSections(IndexSet(integer: 0), with:  .automatic) 
//  }) 

    ref.child("streams").observe(.value, with: { (snapshot) in 

     print("I ran") 

     self.top.removeAll() 
     if let userData = snapshot.value as? NSDictionary { 
      for stream in userData { 
       let newStream = SSStream() 
       newStream.username = stream.key as! String 
       print("Found stream \(stream.key as! String)") 
       newStream.isPrivate = !((stream.value as! NSDictionary)["public"] as! Bool) 
       newStream.views = (stream.value as! NSDictionary)["views"] as! Int 
       newStream.isEnabled = true 
       self.top.append(newStream) 
      } 
     } 
     if self.top.isEmpty { 
      print("No Streams Found") 
      self.top.append(SSStream()) 
     } 
     DispatchQueue.main.async { 
      self.tableView.reloadSections(IndexSet(integer: 0), with: .automatic) 
      self.tableView.reloadData() 
     } 
    }) 
} 

func cellGotPressed(_ stream: String) { 
    self.stream = stream 
    performSegue(withIdentifier: "toPlayer", sender: self) 
} 

func settingsPressed() { 
    performSegue(withIdentifier: "toSettings", sender: self) 
} 

func friendsPressed() { 
    performSegue(withIdentifier: "fromMainToExpandable", sender: self) 
} 

func cameraTapped() { 
    performSegue(withIdentifier: "toRed", sender: self) 
} 

func cellTapped() { 
    print("Cell Tapped") 
} 

// MARK: Segue 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "toPlayer" { 
     let player = segue.destination as! VideoPlayerViewController 
     player.isSubscribing = true 
     player.stream = stream 
    } 
} 

// MARK: Table View Functions 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 3 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "big") as! CategoryRow 
     cell.section = indexPath.section 
     cell.top = top 
     cell.delegate = self 
     cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cameraTapped))) 
     return cell 
    } else { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "small") as! CategoryRow 
     cell.section = indexPath.section 
     cell.recent = recent 
     cell.trending = trending 
     cell.delegate = self 
     return cell 
    } 
} 

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    if section == 0 { 
     return nil 
    } else { 
     let cell = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "MainHeader") 
     let header = cell as! MainHeader 
     if section == 1 { 
      header.fillHeader("RECENT") 
     } else if section == 2 { 
      header.fillHeader("Trending + Now") 
     } else { 
      print("Unknown Section") 
     } 
     return header 
    } 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath.section == 0 { 
     return 300 
    } else { 
     return 100 
    } 
} 

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    if section == 0 { 
     return 0 
    } else { 
     return 50 
    } 
} 

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { 
    return 50 
} 
} 

답변

0

내가해야 할 것을 깨달았습니다. Dispatch.Queue를 호출 할 때

ref.child("streams").observe 

을 설정해야했습니다. 디스패치가 호출되기 전에 참조를 설정하면 프로그램이 제대로 동기화되지 않았습니다. 다음과 같아야합니다.

DispatchQueue.main.async { 
ref.child("streams").observe(.value, with: { (snapshot) in 
     self.tableView.reloadSections(IndexSet(integer: 0), with: .automatic) 
     self.tableView.reloadData() 
    } 
})