I have implemented pull to referesh in application as well as there is pagination.

On pull to refresh action I am fetching page1 data for latest data. and update my main array allObjects with that

Here is model of data and allObjects

final class InspirePost: Codable {

    var id: Int? = nil
   // More properties here 

Here is my try to to this

allObjects is main array with pagination (suppose 5 Pages (1 to 50)) and data is downloaded when pull to refresh done which may contains new + updated + deleted + untouched data

        let copyData = data.reversed()
        for (_,element) in copyData.enumerated() {
            let foundElement = allObjects.filter{$0.id == element.id} // Find  element in main array
            if let firstElement = foundElement.first, let index =   allObjects.index(of: firstElement) {
                allObjects[index] = element // Replace if found
            } else {
                allObjects.insert(element, at: 0) // Insert if not found


        // DELETE item
        let copyAllObject = allObjects
        for (_,element) in copyAllObject.enumerated() {
            guard let id = element.id  else {
            if !data.contains(element) {
                if let indexInMainArray = allObjects.index(where: {$0.id == id}) {
                    allObjects.remove(at: indexInMainArray)


        self.arrayPosts = allObjects

Logic I used is enumerate new data and check existence of element in allData and update or insert object

After that logic is for search deleted data

Please suggest is it good way to do ?

closed as off-topic by Martin R, Stephen Rauch, Ludisposed, yuri, Malachi Sep 18 at 14:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Martin R, Stephen Rauch, Ludisposed, yuri, Malachi
If this question can be reworded to fit the rules in the help center, please edit the question.

  • More context would be helpful. What are the elements in the arrays? Where do data and allObjects come from and what do they contain initially? A concrete (compiling) example would also be good. – Martin R Sep 14 at 15:11
  • @MartinR Thanks for reading question, Actually I have pagination, and pull to refresh too . So allObjects ( Codable class) contains all data that is come from pagination. and data is collection of updated + New object which comes from pull to refresh api. SO Basically I am trying to find each object and replace or insert if required to the allObject from data – Prashant Tukadiya Sep 17 at 4:33
  • @MartinR I am also confused how to handle delete ? – Prashant Tukadiya Sep 17 at 6:58
  • 1
    Code Review is for reviewing working code, not for question on how to implement features. You apparently asked on Stack Overflow. I suggest that (as soon as you have implemented it and it works correctly) you update your question with the working code. Then we can review it and suggest how to improve or simplify your code. Also make sure to add all relevant information to the question, not to the comments. – Martin R Sep 17 at 11:12
  • 1
    Your code adds or updates elements in allObjects from data, and deletes all elements which are not in data. That is the same as assigning allObjects = data. If allObjects has 50 elements, and data contains only the first 10, then – after running your code, allObjects will also have only 10 elements. – Martin R Sep 18 at 4:58

Browse other questions tagged or ask your own question.