Firebase HTTP如何将快照返回到Swift应用程序 [英] How firebase http function to return snapshot to swift app

查看:52
本文介绍了Firebase HTTP如何将快照返回到Swift应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个firebase函数,可以根据geohash搜索半径执行查询和过滤.它正在工作,但是我要返回Json,而我真正想做的就是返回快照.我是Firebase函数的新手.

I have a firebase function performing a query and filtering by a geohash search radius. It's working but I am returning Json and what I really want to do is return a snapshot. I am new to firebase functions.

这是我的firebase函数

Here is my firebase function

var GeoFirestore = require('geofirestore').GeoFirestore;

exports.geoQueryWitningRadius = functions.https.onRequest(async (req, res) => {

 const data = req.body;
 const lat = data.lat;
 const lon = data.lon;
 const radius = data.radius;

 const firestore = admin.firestore();
 const geofirestore = new GeoFirestore(firestore);
 const geocollection = geofirestore.collection('pickups');
 const query = geocollection.near({ center: new admin.firestore.GeoPoint(lat, lon), radius: 
 radius });
 await query.get().then((snapshot) => { 
    return res.send(snapshot)
 }).catch(error => {
     console.log('error', error); 
 });

});

这是我的http请求迅速

Here is my http request in swift

static func getGeoHashPickups(radius: Int, completion: @escaping([String: Any]?, String?) -> Void)  { //url, Error
    
    let parameters : [String : Any] = [
        "lat" : UserService.user.lat,
        "lon" : UserService.user.lon,
        "radius" : radius
    ]
    let url = "https://us-central1-app-request-ect"        
    AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { response in
        switch response.result {
            case .success(let dict):
                guard let dictionary = dict as? [String : Any] else { return }
                completion(dictionary, nil)
            case .failure(let error):
                print(error.localizedDescription)
                completion(nil, error.localizedDescription)
        }
    }
}

如何将前端返回的内容转换为快照?

How do I convert what comes back to my front end to a snapshot?

推荐答案

Cloud Functions无法发送回 DocumentSnapshot ,因为该类型不可序列化.而且无法在Swift代码中将JSON转换回 DocumentSnapshot .

Cloud Functions cannot send back a DocumentSnapshot as that type is not serializable. And there is no way to convert the JSON back to a DocumentSnapshot in the Swift code.

但是您得到的结果归结为该快照的数据,这应该足够了.如果您在返回的数据中丢失了某些内容,则还必须从Cloud Functions代码中返回该其他信息,以便Swift代码可以将其提取.

But what you get boils down to the data of that snapshot, which should be enough. If you're missing something in the data returned, you'll have to also return that additional information from your Cloud Functions code, so the Swift code can pick it up.

这篇关于Firebase HTTP如何将快照返回到Swift应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆