Reload Tableview using RxSwift - ios

I am using RxSwift for tableview. I need to reload my table each time after getting data from api but I'm failed to do this. I couldn't find any solution for that. Can anybody help?
I have an array of places obtain from response of an Api.I have used this code in view did load, but its is not being called when array is updated.

I have found the issue. My array was not being getting updated correctly. I did the following changes.
Declare dataSource variable of ModelClass:
let dataSource = Variable<[SearchResult]>([])
Bind it with the table view right now it is empty:
dataSource.asObservable().bindTo(ResultsTable.rx.items(cellIdentifier: "SearchCell")){ row,Searchplace,cell in
if let C_cell = cell as? SearchTableViewCell{
C_cell.LocationLabel.text = Searchplace.place
}
}.addDisposableTo(disposeBag)
Then store my updated array in it that contains the searchPlaces:
dataSource.value = self.array
Now each time when value of dataSource will be changed, table view will be reloaded.

Avoid using "Variable" because of this concept will be deprecated from RxSwift but official migration path hasn't been decided yet.
REF: https://github.com/ReactiveX/RxSwift/issues/1501
Hence, recommend using RxCocoa.BehaviorRelay instead.
let dataSource = BehaviorRelay(value: [SearchResultModel]())
Bind to tableView
self.dataSource.bind(to: self.tableView.rx.items(cellIdentifier: "SearchCell", cellType: SearchCell.self)) { index, model, cell in
cell.setupCell(model: model)
}.disposed(by: self.disposeBag)
after fetch data:
let newSearchResultModels: [SearchResultModel] = ..... //your new data
dataSource.accept(newSearchResultModels)
Hope this can help :)

Let
array = Variable<[SearchResult]>([])
Whenever you hit your API, put your fetched results in self.array.value and it will automatically gets updated.
self.array.asObservable().bindTo(ResultsTable.rx.items(cellIdentifier: "SearchCell", cellType:SearchCell.self))
{ (row, element, cell) in
cell.configureCell(element: element)
}.addDisposableTo(disposeBag)

Related

Convert the original tableview to collectionview data cannot be passed

I want to replace the original tableview with collectionview, the code of the original tableview:
let selectedRow = MarketView.indexPathForSelectedRow!.row
I'm learning online to change to this code and I get an error:
let selectedRow = MarketView.indexPathsForSelectedItems!.first
The error shows:
Cannot convert value of type 'IndexPath?' to expected argument type 'Int'
This is the complete code as shown in the figure
I just learned to use collectionview, how should I modify it, thank you for your help
Unlike indexPathForSelectedRow which returns a single index path indexPathsForSelectedItems returns an array of index paths
And row is not first, the collection view equivalent of row – as the name of the API implies – is item, you have to write indexPathsForSelectedItems!.first!.item.
But it's not recommended to force unwrap the objects. A safer way is
guard let selectedRow = MarketView.indexPathsForSelectedItems?.first?.item else { return }

Firebase query observing reshowing data

I have a firebase query that observes data from a posts child.
func fetchPosts () {
let query = ref.queryOrdered(byChild: "timestamp").queryLimited(toFirst: 10)
query.observe(.value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
if let value = child.value as? NSDictionary {
let post = Post()
let poster = value["poster"] as? String ?? "Name not found"
let post_content = value["post"] as? String ?? "Content not found"
let post_reveals = value["Reveals"] as? String ?? "Reveals not found"
post.post_words = post_content
post.poster = poster
post.Reveals = post_reveals
self.postList.append(post)
DispatchQueue.main.async { self.tableView.reloadData() }
//make this for when child is added but so that it also shows psots already there something like query.observre event type of
}
}
However, when a user posts something, it creates a more than one cell with the data. For instance, if I post "hello", a two new cards show up with the hello on it. However, when I exit the view and recall the fetch posts function, it shows the correct amount of cells. Also, when I delete a post from the database, it adds a new cell as well and creates two copies of it until I reload the view, then it shows the correct data from the database.
I suspect this has something to do with the observe(.value), as it might be getting the posts from the database and each time the database changes it creates a new array. Thus, when I add a new post, it is adding an array for the fact that the post was added and that it now exists in the database, and when I refresh the view it just collects the data directly from the database.
Also, sometimes the correct amount of cells show and other times there's multiple instances of random posts, regardless of whether I have just added them or not.
How can I change my query so that it initially loads all the posts from the database, and when some post is added it only creates one new cell instead of two?
Edit: The logic seeming to occur is that when the function loads, it gets all the posts as it calls the fetchPosts(). Then, when something is added to the database, it calls the fetchPosts() again and adds the new data to the array while getting all the old data. yet again.
One thing I always do when appending snapshots into an array with Firebase is check if it exists first. In your case I would add
if !self.postList.contains(post) {
self.postList.append...
however, to make this work, you have to make an equatable protocol for what I'm guessing is a Post class like so:
extension Post: Equatable { }
func ==(lhs: Post, rhs: Post) -> Bool {
return lhs.uid == rhs.uid
}
You are right in thinking that the .value event type will return the entire array each time there is a change. What you really need is the query.observe(.childAdded) listener. That will fetch individual posts objects rather than the entire array. Call this in your viewDidAppear method.
You may also want to implement the query.observe(.childRemoved) listener as well to detect when posts are removed.
Another way would be to call observeSingleEvent(.value) on the initial load then add a listener query.queryLimited(toLast: 1).observe(.childAdded) to listen for the latest post.

Creating default data for an entity in CoreData (Swift)

I want my app to have some default data so the user isn't presented with an empty tableView.
The data needs to be on coreData, and then fetched and put onto the the tableView.
At the moment, the user can add some data, delete it and edit it, but when they first open the app there isn't any data, which doesn't look great and might confuse the user.
The default data should be deletable and editable like the rest of the data, so it can't be "false data", like strings stored in an array.
How can I add some default data using Swift?
The cellforRowatindexpath method defines the cell for your table. If you want some default cells you can define them there. Either create some custom cells with false data, or (this is better) define an array or dictionary in your tableView class with false data, and use it to feed your table's delegate and datasource methods (numberOfRows/numberOfSections need input, as well). Basic example:
CustomUIViewController : UIViewController {
let defaults : [String] = ["Some", "Data"]
self.tableView.dataSource = self
self.tableView.delegate = self
func cellForRowAtIndexPath() -> UITableViewCell {
var cell = UITableViewCell()
if (// check for non-nil data here) {
// configure cell based on loaded data
} else {
cell.textLabel?.text = defaults[indexPath.row]
}
return cell
}
Let me know if you have any questions about this. Your question is vague, so I kept my answer similar.

Displaying data in a tableView from a filtered array

I have written code that filters events from Core Data and only prints the events that have a date attribute that is equal to a date that was selected in a Calendar. Heres the code:
let predicate = NSPredicate(format: "eventPastStringDate = %#", formatter.stringFromDate(selectedDate))
//This prints the exact data that I want
print((eventsPast as NSArray).filteredArrayUsingPredicate(predicate))
This works and it filters that data how I would like and it prints only the events that I want. The problem is that I do not know how to display this data in the tableView. Usually I can display all the data from Core Data in the tableView like this in cellForRowAtIndexPath...
let ePast = eventsPast[indexPath.row]
let cellPast = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
// Sets labels in cell to whatever user typed in on the events page
cellPast.titlePrototypeCell!.text = ePast.eventPastTitle!
cellPast.datePrototypeCell!.text = "Date: " + ePast.eventPastDateLabel!
return cellPast
...but I am not sure how to access the new data as an array like I did above. Any ideas?
You are going to have a second array in your class. When there is no filter, the two arrays are identical. When there is a filter, then you have a master array with absolutely everything and the filtered array is a small subset.
(class var) myFilteredArray = (eventsPast as NSArray).filteredArrayUsingPredicate(predicate)
Then in your tableview methods:
let ePast = myFilteredArray[indexPath.row]
And make sure to set the table row size to your filtered array count, not the master array count. Otherwise you are going to get out of bounds crashes.

Use Realm with Collection View Data Source Best Practise

I'll make it short as possible.
I have an API request that I fetch data from (i.e. Parse).
When I'm getting the results I'm writing it to Realm and then adding them to a UICollectionView's data source.
There are requests that take a bit more time, which run asynchronous. I'm getting the needed results after the data source and collection view was already reloaded.
I'm writing the needed update from the results to my Realm database.
I have read that it's possible to use Realm's Results. But I honestly didn't understood it. I guess there is a dynamic and safe way working with collection views and Realm. Here is my approach for now.
This is how I populate the collection view's data source at the moment:
Declaration
var dataSource = [Realm_item]()
where Realm_item is a Realm Object type.
Looping and Writing
override func viewDidLoad() {
super.viewDidLoad()
for nowResult in FetchedResultsFromAPI
{
let item = Realm_item()
item.item_Title = nowResult["Title"] as! String
item.item_Price = nowResult["Price"] as! String
// Example - Will write it later after the collectionView Done - Async request
GetFileFromImageAndThanWriteRealm(x.image)
// Example - Will write it later after the collectionView Done - Async request
dataSource.append(item)
}
//After finish running over the results *Before writing the image data*
try! self.realm.write {
self.realm.add(self.dataSource)
}
myCollectionView.reloadData()
}
After I write the image to Realm to an already created "object". Will the same Realm Object (with the same primary key) automatically update over in the data source?
What is the right way to update the object from the data source after I wrote the update to same object from the Realm DB?
Update
Model class
class Realm_item: Object {
dynamic var item_ID : String!
dynamic var item_Title : String!
dynamic var item_Price : String!
dynamic var imgPath : String?
override class func primaryKey() -> String {
return "item_ID"
}
}
First I'm checking whether the "object id" exists in the Realm. If it does, I fetch the object from Realm and append it to the data source. If it doesn't exist, I create a new Realm object, write it and than appending it.
Fetching the data from Parse
This happens in the viewDidLoad method and prepares the data source:
var query = PFQuery(className:"Realm_item")
query.limit = 100
query.findObjectsInBackgroundWithBlock { (respond, error) -> Void in
if error == nil
{
for x in respond!
{
if let FetchedItem = self.realm.objectForPrimaryKey(Realm_item.self, key: x.objectId!)
{
self.dataSource.append(FetchedItem)
}
else
{
let item = Realm_item()
item.item_ID = x.objectId
item.item_Title = x["Title"] as! String
item.item_Price = x["Price"] as! String
let file = x["Images"] as! PFFile
RealmHelper().getAndSaveImageFromPFFile(file, named: x.objectId!)
self.dataSource.append(item)
}
}
try! self.realm.write {
self.realm.add(self.dataSource)
}
self.myCollectionView.reloadData()
print(respond?.count)
}
}
Thank you!
You seem to have a few questions and problems here, so I'll do my best.
I suggest you use the Results type as your data source, something like:
var dataSource: Results<Realm_item>?
Then, in your viewDidLoad():
dataSource = realm.objects(Realm_item).
Be sure to use the relevant error checking before using dataSource. We use an optional Results<Realm_item> because the Realm object you're using it from needs to be initialised first. I.e., you'll get something like "Instance member * cannot be used on type *" if you try declaring the results like let dataSource = realm.objects(Realm_item).
The Realm documentation (a very well-written and useful reference to have when you're using Realm as beginner like myself), has this to say about Results...
Results are live, auto-updating views into the underlying data, which means results never have to be re-fetched. Modifying objects that affect the query will be reflected in the results immediately.
Your mileage may vary depending on how you have everything set up. You could try posting your Realm models and Parse-related code for review and comment.
Your last question:
What is the right way to update the "object" from the Data Source after i wrote the update to same object from the Realm DB?
I gather you're asking the best way to update your UI (CollectionView) when the underlying data has been updated? If so...
You can subscribe to Realm notifications to know when Realm data is updated, indicating when your app’s UI should be refreshed for example, without having to re-fetch your Results.

Resources