2016-08-27 9 views
0

신속하고 firebase를 사용하는 iOS 응용 프로그램에서 작업하고 있습니다.firebase에 사용자 정보 업로드 오류 (빠른 프로젝트)

사용자는 자신의 이름, 이메일, 비밀번호 및 기타 정보로 등록보기에서 등록 할 수 있습니다. 대시 보드의 사용자 노드는 각 사용자에게 ID를 저장하는 user-devices라는 하위 노드가 있어야합니다. '사용자 장치 용 :

 { 
     "Users": { 
     "1EJ8QmEQBJfBez7PMADbftCjVff1":{ 
     "Email":"", 
     "Phone":"", 
     "Name":"", 
     "City":"", 
     "UserDevices": { 
     "1EJ8QmEQBJfBez7PM":"", 
     "1EJ8QmEQBJfBdfkgj":"", 
     "1EJ8QmEQBJfBdfkgj":"" 
     } 
    }, 
     "97PUAcUC5UYxLpBOLnC4yQjxiEf2": { 
     "Email":"", 
     "Phone":"", 
     "Name":"", 
     "City":"", 
     "UserDevices": { 
     "1EJ8Qgjhdghhgjhgd":"", 
     "sjdhfkhfhfhBdfkgj":"" 
     } 
    } 
    } 
    } 

등록 된 사용자가 장치를 추가 할 수있는 다른 장치보기가 있습니다. 그/그녀는 장치 이름, 설명, 범주를 입력하고 이미지를 추가 한 다음 장치 추가 버튼을 클릭하기 만하면됩니다.

각 사용자는 5 개의 장치 만 추가 할 수 있습니다. 나는 코드로이 구조를 설정하는 시도

"Devices": { 
    "1EJ8QmEQBJfBez7PMADbftCjVff1":{ 
    "DeviceName": "", 
    "Description": "", 
    "Category": "", 
    "ImageURL": "" 
    }, 
    "97PUAcUC5UYxLpBOLnC4yQjxiEf2":{ 
    "DeviceName": "", 
    "Description": "", 
    "Category": "", 
    "ImageURL": "" 
} 
} 

:

그래서 같은 장치라는 또 다른 노드가 있습니다. 나는 문제가있을 경우

else { 

        FIRAuth.auth()?.signInWithEmail(self.EmailTF.text!, password: self.PasswordTF.text!, completion: { (user: FIRUser?, error: NSError?) in 
         if let error = error { 
          print(error.localizedDescription) 
         } else { 

          self.ref.child("UserProfile").child(user!.uid).setValue([ 
           "email": self.EmailTF.text!, 
           "name" : self.NameTF.text!, 
           "phone": self.PhoneTF.text!, 
           "city" : self.CityTF.text!, 

           ]) 
          self.ref.child("UserProfile").child("UserDevices") 
          print("Sucess") 

         } 

그리고 여기에 추가 장치 버튼의 부분 : 여기

은 등록 버튼의 일부

else { 


     let imageName = NSUUID().UUIDString 
     let storageRef = FIRStorage.storage().reference().child("Devices_images").child("\(imageName).png") 

     if let uploadData = UIImagePNGRepresentation(self.ImageView.image!) { 
      storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in 
       if error != nil { 
        print(error) 

       } else { 

        //Here I want to check of the number of the ids' of UserDevices if less than or equal to 5 the user can add device, otherwise an alert will appear to the user! 
       if self.ref.child("UserProfile").child("UserDevices").child(id) <= 5 { 

       let profileImageUrl = metadata?.downloadURL()?.absoluteString 
       let DeviceInfo = [ 
         "ImageUrl":profileImageUrl!, 
         "DeviceName":self.DeviceName.text!, 
         "Description":self.Description.text!, 
         "Category":self.itemSelected 
         ] 
        self.ref.child("Devices").childByAutoId().setValue(DeviceInfo) 
        self.ref.child("UserProfile").child("UserDevices").setValue(id) 

        } else { 

       print("You can only enter five devices maximum") 
         //alert message 

        } 
       } 

      }) 





} 

} 

*이은에 액세스 할 수있는 올바른 방법인가 UserDevices ids '?

self.ref.child("UserProfile").child("UserDevices").child(id) 

답변

1

이런 식으로 뭔가를 시도 : -

let DeviceInfo = [ 
        "ImageUrl":profileImageUrl!, 
        "DeviceName":self.DeviceName.text!, 
        "Description":self.Description.text!, 
        "Category":self.itemSelected 
        ] 

    self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEventOfType(.Value, withBlock: {(snapShot) in 

     if snapShot.exists(){ 

      let numberOfDevicesAlreadyInTheDB = snapShot.childrenCount 

      if numberOfDevicesAlreadyInTheDB < 5{ 

       let newDevice = String("Device\(numberOfDevicesAlreadyInTheDB+1)") 

       let userDeviceRef = self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid) 

       userDeviceRef.observeSingleEventOfType(.Value, withBlock: {(userDevices) in 

        if let userDeviceDict = userDevices.value as? NSMutableDictionary{ 

         userDeviceDict.setObject(DeviceInfo,forKey: newDevice) 

         userDeviceRef.setValue(userDeviceDict) 
        } 
       }) 
      } 
       else{ 
        //Show Alert that user cant enter Devices Anymore 
       } 
      }else{ 
       self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).setValue(["Device1" : DeviceInfo]) 
      } 
     }) 
+0

찬성 투표가 도움이 경우이 답 :) 해피 파일로 저장하는 이미지, 그것은 습관이 DB입니다 cource의 – Dravidian

+0

코딩 당신이 중포 기지에 보관해야 저장 ... Firebase Realtime Datbase에서 할 수있는 일은 filePath 나 url을이 경우 성공적으로 DB에 저장 한 다음 이미지를 검색하거나 심지어 doenload 할 때 사용합니다. – Dravidian

+0

'url'은 경로를 의미합니다. Firebase Storage에 저장된 이미지의 ..? 맞아. 예라면 이미지를 다운로드하기 전에 Firebase 데이터베이스에 저장된 이미지의 경로를 검색해야합니다. 그런 다음 해당 경로를 사용하여 Firebase Storage를 탐색 한 다음 다운로드하십시오. – Dravidian

관련 문제