2017-12-16 2 views
0

코드가 thread safe한지 궁금합니다. tableView (_ tableView :, leadingSwipeActionsConfigurationForRowAt indexPath :) 친구 요청을 수락하는 작업을 만듭니다. (: .normal, 제목 : 스타일 전무) 방법은 UIContextualAction의 BLOK에서 호출 {에서 (액션,보기, 핸들러)}Firebase에 iOS를 작성하면 충돌이 발생합니다.

실제 중포 기지 호출은 다음과 같이이다 :

class func acceptInvite(uid: String, completion: @escaping (Bool)->Void) { 
    guard let user = currentUser else { completion(false); return } 
    usersRef.child(user.uid).child("invites").queryEqual(toValue: uid).ref.removeValue() 
    usersRef.child(user.uid).child("friends").childByAutoId().setValue(uid) 
    usersRef.child(uid).child("friends").childByAutoId().setValue(user.uid) 
    completion(true) 
} 

image from debug navigator

누군가 설명이 있다면 좋을 것입니다.

편집 : 나는 문제이 문제는 정말 중포 기지 사용에 대한 생각을 저를 얻는 유저 데이터

class func get(type: String, completion: @escaping ([Friend])->Void) { 
    let usersRef = Database.database().reference().child("users") 
    guard let user = currentUser else { completion([]); return } 
    usersRef.child(user.uid).child(type).observe(.value){ (snapshot) in 
     guard let invitesKeyValues = snapshot.value as? [String: String] else { completion([]); return } 
     var optionalFriendsDictArray: [[String: Any]?] = [] 
     let dispatchGroup = DispatchGroup() 

     for (_, inviteUID) in invitesKeyValues { 
      dispatchGroup.enter() 
      usersRef.child(inviteUID).observe(.value, with: { (snapshot) in 
       let friend = snapshot.value as? [String: Any] 
       optionalFriendsDictArray.append(friend) 
       dispatchGroup.leave() 
      }) 
     } 
     dispatchGroup.notify(queue: DispatchQueue.global(), execute: { 
      let friends = optionalFriendsDictArray.flatMap({ (optional) -> Friend? in 
       Friend.init(userDictionary: optional) 
      }) 
      completion(friends) 
     }) 
    } 
} 

을 얻기 위해 내 비동기 루프라고 생각합니다. 사용자의 친구 키에 사용자에 대한 추가 정보를 추가 할 수 있으므로 모든 사용자에게 이름과 사진으로 작은 목록을 채우도록 쿼리하지 않아도됩니다. 하지만 타임 라인에서 친구들의 게시물을 보는 것은 어떻습니까? 확실히 모든 친구의 게시물을 사용자 개체에 복사하지 않을 것입니다. ???

+0

이것은 사용자의 관찰자와 관련이 있습니다. 관측자는 관측중인 사용자에게 편지를 쓰려고 할 때 충돌의 원인 인 것 같습니다. –

+0

크래시 로그에 대한 세부 정보를 공유 할 수 있습니까? –

+0

로그에 아무것도 없습니다. 디버그 탐색기를보고있는 스레딩 문제라고 생각합니다. –

답변

1

관찰 단일 이벤트로 데이터를 가져 와서 childadded 및 childremoved 옵저버에서 돌연변이를 사용하여이 문제를 해결했습니다.

관련 문제