ios – SwiftUI: Objects not marking as learn when making an attempt to navigate previous final web page

0
15
ios – SwiftUI: Objects not marking as learn when making an attempt to navigate previous final web page


I’ve a SwiftUI app that hundreds paginated information from Firestore. When customers navigate between pages utilizing a “Subsequent” button, gadgets are mechanically marked as learn. Nonetheless, when customers are on the final web page and try to navigate additional (despite the fact that there aren’t any extra gadgets), the present gadgets do not get marked as learn.

Here is my present loadNextPage perform in my FirestoreDataViewModel:

func loadNextPage() {
    guard let lastDocument = lastDocument, canLoadMore else {
        return
    }
    
    markUnreadItemsAsRead()
    isLoading = true
    currentPage += 1
    
    var question = db.assortment("gadgets")
        .order(by: "timestamp", descending: true)
        .begin(afterDocument: lastDocument)
        
    // Apply filters
    if showFilterA {
        question = question.whereField("filterA", isEqualTo: true)
    }
    // ... different filters
    
    question.restrict(to: itemsPerPage).getDocuments { snapshot, error in
        self.isLoading = false
        if let error = error {
            self.errorMessage = error.localizedDescription
            return
        }
        
        guard let snapshot = snapshot else {
            self.errorMessage = "No extra information accessible"
            return
        }
        
        let newItems = snapshot.paperwork.compactMap { self.documentToItem($0) }
        self.gadgets = newItems
        self.lastDocument = snapshot.paperwork.final
        self.canLoadMore = snapshot.paperwork.depend == self.itemsPerPage
    }
}

What I’ve Tried

  1. Shifting markUnreadItemsAsRead() earlier than the guard assertion:
func loadNextPage() {
    markUnreadItemsAsRead()
    guard let lastDocument = lastDocument, canLoadMore else {
        return
    }
    // ... remainder of the perform
}
  1. Immediately updating Firestore within the guard assertion:
func loadNextPage() {
    guard let lastDocument = lastDocument, canLoadMore else {
        let unreadItems = gadgets.filter { !$0.learn }
        for merchandise in unreadItems {
            db.assortment("gadgets").doc(merchandise.id).updateData(["read": true])
        }
        return
    }
    // ... remainder of the perform
}
  1. Utilizing a separate perform for marking gadgets as learn:
func markUnreadItemsAsRead() {
    let unreadItems = gadgets.filter { $0.learn == false }
    for merchandise in unreadItems {
        db.assortment("gadgets").doc(merchandise.id).updateData(["read": true]) { error in
            if let error = error {
                print("Error marking merchandise as learn: (error.localizedDescription)")
            } else {
                DispatchQueue.essential.async {
                    if let index = self.gadgets.firstIndex(the place: { $0.id == merchandise.id }) {
                        self.gadgets[index].learn = true
                    }
                }
            }
        }
    }
}

None of those approaches efficiently mark gadgets as learn when making an attempt to navigate previous the final web page.

Anticipated Habits

When a consumer is on the final web page and faucets the “Subsequent” button, despite the fact that there aren’t any extra gadgets to load, the present gadgets needs to be marked as learn in each Firestore and the native state.

Query

How can I modify my code to make sure gadgets are marked as learn when making an attempt to navigate previous the final web page? I want an answer that:

  1. Works with the prevailing pagination system
  2. Updates each Firestore and native state
  3. Handles the final web page case appropriately

LEAVE A REPLY

Please enter your comment!
Please enter your name here