数组元素不断追加

时间:2017-08-20 03:11:11

标签: ios swift firebase firebase-realtime-database

我从firebase查询并且元素正常返回。但是,当我搜索两次时会出现两个元素。这意味着两个元素被附加到数组。应该只是一个从搜索中回来的东西。如果我搜索两次,就不应该有两个元素。

 if self.postsArray.count == 0
           {
               self.postsArray.append(newPost!)


               self.filteredPosts = self.postsArray.filter { (post) -> Bool in
                   return post.title.lowercased().contains(stringValue.lowercased())

               }
               DispatchQueue.main.async {
                   self.exploreCollectionView?.reloadData()
               }

           }
           else
           {

               for post in self.postsArray
               {
                   if post.key == newPost?.key{

                       self.filteredPosts = self.postsArray.filter { (post) -> Bool in
                           return post.title.lowercased().contains(stringValue.lowercased())

                       }
                       DispatchQueue.main.async {
                           self.exploreCollectionView?.reloadData()
                       }
                       return
                   }
                   else
                   {
                       self.postsArray.append(newPost!)
                       //                            self.postsArray.sort(by: { (post1, post2) -> Bool in
                       //                                return post1.title.compare(post2.title) == .orderedAscending
                       //                            })

                       self.filteredPosts = self.postsArray.filter { (post) -> Bool in
                           return post.title.lowercased().contains(stringValue.lowercased())

                       }
                       DispatchQueue.main.async {
                           self.exploreCollectionView?.reloadData()
                       }

                   }

               }

           }

所以我很好地询问是否有人可以看一下这个,看看我的逻辑中的错误在哪里使得多次将事物添加到过滤的事件数组中

1 个答案:

答案 0 :(得分:1)

我猜问题是for for循环,每当新帖子不相等时你就会将新帖子附加到数组中,而它只应该附加一次

因此,您可以使用过滤器来查看它是否包含newPost

所以替换

for post in self.postsArray
               {
                   if post.key == newPost?.key{

                       self.filteredPosts = self.postsArray.filter { (post) -> Bool in
                           return post.title.lowercased().contains(stringValue.lowercased())

                       }
                       DispatchQueue.main.async {
                           self.exploreCollectionView?.reloadData()
                       }
                       return
                   }
                   else
                   {
                       self.postsArray.append(newPost!)
                       //                            self.postsArray.sort(by: { (post1, post2) -> Bool in
                       //                                return post1.title.compare(post2.title) == .orderedAscending
                       //                            })

                       self.filteredPosts = self.postsArray.filter { (post) -> Bool in
                           return post.title.lowercased().contains(stringValue.lowercased())

                       }
                       DispatchQueue.main.async {
                           self.exploreCollectionView?.reloadData()
                       }

                   }

let filteredArr = self.postsArray.filter { (post) -> Bool in
                           return post.key == newPost?.key
}

if filteredArr.count == 0 {
     //append
     self.postsArray.append(newPost!)
} 


 self.filteredPosts = self.postsArray.filter { (post) -> Bool in
                           return post.title.lowercased().contains(stringValue.lowercased())

                       }
                       DispatchQueue.main.async {
                           self.exploreCollectionView?.reloadData()
                       }