2016-11-25 57 views
0

嗨我想通過ParseLiveQuery獲取我的對象ParseLiveQuery無法訪問指針對象

我認爲ParseLiveQuery不支持指針對象。

這是我的代碼片段。

Post.swift

import Foundation 
import Parse 

class Post: PFObject, PFSubclassing { 

    @NSManaged var postBy: PFUser? 
    @NSManaged var postText: String? 
    @NSManaged var postImg: PFFile? 


    static func parseClassName() -> String { 
     return "Post" 
    } 



    override class func query() -> PFQuery<PFObject>? { 
     //1 
     let query = PFQuery(className: Post.parseClassName()) 

     query.whereKeyExists("objectId") 
     //2 
     query.includeKeys(["postBy"]) 
     //3 
     query.order(byDescending: "createdAt") 
     return query 
    } 

} 

extension Post: FeedCellSupport { 

    var username:String?{ 

     return postBy?["username"] as? String 


    } 

    var userImg:PFFile?{ 

     return postBy?["profileImg"] as? PFFile 
    } 

FeedVC.swif

let liveQueryClient = ParseLiveQuery.Client() 

    protocol FeedCellSupport { 

     var username:String?{ get } 
     var userImg:PFFile?{ get } 


     var postText: String? { get } 

    } 


class FeedVC: UITableViewController, ShopDetailDelegate { 

    private var subscription: Subscription<Post>! 

    //Add friends and me 
    var postLiveQuery: PFQuery<Post> { 
     return (Post.query()? 
      .whereKeyExists("objectId") 
      .includeKeys(["postBy", "commentBy", "commentBy.commentBy"]) 
      .order(byAscending: "createdAt")) as! PFQuery<Post> 
    } 


    var results = [Post]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
subscription = liveQueryClient.subscribe(postLiveQuery).handleSubscribe { [weak self] (_) in 
      // Fetch the objects on subscription to not miss any 
      self?.fetchObjects() 
      }.handleEvent { [weak self] (_, event) in 
       self?.handleEvent(event: event) 
     } 

} 

我有2個問題。

1. ParseLiveQuery返回的PFObject類似於REST結果。

當我第一次獲取對象它工作正常。

我的意思是我可以通過findObjectBackground獲取用戶名和userImg數據。

<Post: 0x6180000b43a0, objectId: w5qcfMCByF, localId: (null)> { 
likeCount = 0; 
postBy = "<PFUser: 0x6180000f4600, objectId: rgG7XfAYWj, localId: (null)>"; 
postImg = "<PFFile: 0x618000246ba0>"; 
postText = nice2MeetYous121; 
sell = 0; 
} 

在收到更新的事件後,我得到了不同的樣式PFObject。

<Post: 0x6100000b3020, objectId: w5qcfMCByF, localId: (null)> { 
"__type" = Object; 
className = Post; 
createdAt = "2016-11-19T12:37:30.896Z"; 
likeCount = 0; 
postBy = { 
"__type" = Pointer; 
className = "_User"; 
objectId = rgG7XfAYWj; 
}; 
postImg = { 
"__type" = File; 
name = "92aa66bfdcf5f70d1d277556bbd9d7ca_post.jpg"; 
url = "https://parsefiles.back4app.com/PlY72cbRxsOIXQ1qbJjaQtudZQU9HA8U2RIg2oE1/92aa66bfdcf5f70d1d277556bbd9d7ca_post.jpg"; 
}; 
postText = nice2MeetYous1211; 
sell = 0; 
updatedAt = "2016-11-21T14:34:11.338Z"; 
} 

因此,我收到了一條錯誤消息,因爲Parse LiveQuery像REST一樣返回。

2.當我嘗試了「fix-object-decode」的分支時,它返回了PFObject而不是REST樣式。但它也不能直接檢索我的指針數據。

ios ParseLiveQuery link branch->fix-object-decode

我曾測試 「修復對象解碼」 分支。

它返回格式良好的PFObject,但它看起來不同的結果。

這是我原來的PFObject當我通過findObjectInBackGround要求()

<Post: 0x6080000b0800, objectId: w5qcfMCByF, localId: (null)> { 
likeCount = 0; 
postBy = "<PFUser: 0x6080000e3100, objectId: rgG7XfAYWj, localId: (null)>"; 
postImg = "<PFFile: 0x60800024c150>"; 
postText = nice2MeetYous1211; 
sell = 0; 
} 

當我改變postText然後我通過實況查詢,但不同了更新事件。

<Post: 0x6100000b1be0, objectId: w5qcfMCByF, localId: (null)> { 
likeCount = 0; 
postBy = "<PFUser: 0x6100000e6900, objectId: rgG7XfAYWj, localId: (null)>"; 
postImg = "<PFFile: 0x61000025d1f0>"; 
postText = nice2MeetYous; 
sell = 0; 
} 

正如你可以see..PFUser:0x6080000e3100和PFFile:0x60800024c150已更改爲PFUser:0x6100000e6900,PFFile:0x61000025d1f0

反正當我接收到的事件,我不能fetchObject。

這是關於活查詢或分析服務器端問題的問題嗎?

由於未捕獲異常而終止應用程序'NSInternalInconsistencyException',原因:'密鑰「用戶名」沒有數據。在獲取其值之前調用fetchIfNeeded。'

我添加了一些代碼以避免fetchIfNeeded錯誤。

這是我的代碼片段。

extension Post: FeedCellSupport { 

var username:String?{ 

    do { 
     try postBy?.fetchIfNeeded() 
    } catch _ { 
     print("There was an error") 
    } 


    return postBy?["username"] as? String 


} 

var userImg:PFFile?{ 
    do { 
     try postBy?.fetchIfNeeded() 
    } catch _ { 
     print("There was an error") 
    } 

    return postBy?["profileImg"] as? PFFile 
} 

現在的作品不錯,但......系統提示「警告:正在主線程上執行的長期運行的操作」

我想是不是百威..

你能幫我嗎?

回答

0

我注意到解析服務器不支持includekey。

+0

你介意分享鏈接嗎? – nathan

+0

更新我的評論。 includeKey工作正常。 https://github.com/parse-community/ParseLiveQuery-iOS-OSX/issues/91 –

+0

當然? https://github.com/parse-community/ParseLiveQuery-iOS-OSX/issues/30和https://github.com/parse-community/parse-server/issues/1686 – nathan