可观察的混乱 [英] Observable Confusion

查看:145
本文介绍了可观察的混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ionic2 AngularFire2



我也正在使用 rxjs Observable 。我有以下代码:
$ b $ pre $ findChatsForUid(uid:string):Observable< any []> {
return this.af.database.list('/ chat /',{
query:{
orderByChild:'negativtimestamp'
}
})。map (item => {
const filtered = items.filter(
item =>(item.memberId1 === uid || item.memberId2 === uid)
);
返回过滤;
});

code $

$ b $ p

$ pre> deleteChatsAndMessagesForUid(uid:string):Promise< any> {
return new Promise< any>((resolve)=> {
let promiseArray:Promise< any> [] = [];
this.findChatsForUid(uid).map(items forEach((chatItems)=> {
for(let i:number = 0; i< chatItems.length; i ++){
promiseArray.push(this.deleteChat(chatItems [i],true));
}
Promise.all(promiseArray).then(()=> {
resolve (true);
});
});
});



$ b $ p
$ b

在第二个函数中,可以看到我检索 Observable ,并使用 forEach 函数循环遍历每个项目。



我的问题是,因为这是一个 Observable ,总是有对象的句柄。所以,当我做到以下几点:$ b​​
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ code $ deleteChatsAndMessagesForUid(uid).then(()=> {
用户。 delete()。then(()=> {
...
}
}

由于被删除的用户仍然在观察 Observable ,导致出现以下错误。


错误:未被捕获(在promise中):错误:permission_denied在/ chat:
客户端没有访问所需数据的权限错误:
permission_denied at / chat:客户无权访问
所需的数据。


问题

有没有一种方法可以检索数据,而不会连接到 Observable ?这样我就可以自由删除关联的用户?还是有更好的方法来处理这个?



谢谢

解决方案

这听起来像你想取消sribe从 list > observable在第一个发出的列表之后。

您可以使用 操作符在第一个发出列表之后完成 list observable。这将导致自动取消订阅,并将侦听器从内部Firebase文档中移除。
$ b

  import 'rxjs /添加/操作者/第一'; 

findChatsForUid(uid:string):Observable< any []> {
return this.af.database
.list('/ chat /',{
query:{
orderByChild:'negativtimestamp'
}
}}
.first()
.map(items => {
const filtered = items.filter(
item =>(item.memberId1 === uid | | item.memberId2 === uid)
);
return filtered;
});
}


I am using Ionic2 with AngularFire2.

I am also making use of a rxjs Observable. I have the following code:

findChatsForUid(uid: string): Observable<any[]> {
    return this.af.database.list('/chat/', {
        query: {
            orderByChild: 'negativtimestamp'
        }
    }).map(items => {
        const filtered = items.filter(
            item => (item.memberId1 === uid || item.memberId2 === uid)
        );
        return filtered;
    });
}

and

deleteChatsAndMessagesForUid(uid: string): Promise<any> {
    return new Promise<any>((resolve) => {
        let promiseArray: Promise<any>[] = [];
        this.findChatsForUid(uid).map(items => {
            return items;
        }).forEach((chatItems) => {
            for (let i: number = 0; i < chatItems.length; i++) {
                promiseArray.push(this.deleteChat(chatItems[i], true));
            }
            Promise.all(promiseArray).then(() => {
                resolve(true);
            });
        });
    });
}

In the second function, you can see I retrieve the Observable from the first, and the loop through each item using the forEach function.

My problem is, because this is an Observable, there is always a handle to the object. So when I do the following:

deleteChatsAndMessagesForUid(uid).then(() => {
    user.delete().then(() => {
        ...
    }
}

It results in the following error because the deleted user is still trying to observe the Observable.

Error: Uncaught (in promise): Error: permission_denied at /chat: Client doesn't have permission to access the desired data. Error: permission_denied at /chat: Client doesn't have permission to access the desired data.

Question

Is there a way to retrieve the data, without still being attached to the Observable? So that I am free to delete the associated user? Or is there a better way to handle this?

Thanks

解决方案

It sounds like you want to unsubsribe from the list observable after the first emitted list.

You can use the first operator to complete the list observable after the first emitted list. This will result in automatic unsubscription and the listeners will be removed from the internal Firebase ref.

import 'rxjs/add/operator/first';

findChatsForUid(uid: string): Observable<any[]> {
  return this.af.database
    .list('/chat/', {
      query: {
        orderByChild: 'negativtimestamp'
      }
    })
    .first()
    .map(items => {
      const filtered = items.filter(
        item => (item.memberId1 === uid || item.memberId2 === uid)
      );
      return filtered;
    });
}

这篇关于可观察的混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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