2

I'm using core data to store my master data and update it based on user interaction view UI related to same.

First, I'm fetching some data in SignupStepperVC using following method and pass it to SignupGroupVC which is embed in UIContainerView inside SignupStepperVC.

So my code in SignupStepperVC would be:

self.arrSubCategory = TblSubCategory.fetchSubCategories()

where fetchSubCategories() is class method of TblSubCategory which will fetch all sub-categories from entity and return to VC where call this.

@nonobjc public class func nsFetchRequest() -> NSFetchRequest<TblSubCategory> {
    return NSFetchRequest<TblSubCategory>(entityName: "TblSubCategory")
}

class func fetchSubCategories() -> [TblSubCategory]? {

    let fetchRequest = TblSubCategory.nsFetchRequest()
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "sName", ascending: true)]

    do {
        let arrSubCategories = try AppDelegate.shared.persistentContainer.viewContext.fetch(fetchRequest)
        return arrSubCategories

    } catch {
        Logger.error("TblSubCategory - \(error.localizedDescription)")
        return nil
    }
}

TblSubCategory has following properties:

@NSManaged public var bIsSelected           : Bool
@NSManaged public var nId                   : Int16
@NSManaged public var nMainCategoryId       : Int16
@NSManaged public var nIsActive             : Int16
@NSManaged public var nGroupId              : Int16
@NSManaged public var sName                 : String?
@NSManaged public var sImage                : String?

Now using the ref of 'SignupGroupVC' I'm passing array of sub-category to it.

self.refSignupGroupVC?.arrSubCategory = self.arrSubCategory

In SignupGroupVC, I'm have list of group, so when select any group, related to that group all category will display in SignupFirstStepVC.

self.signupFirstStepVC?.arrSubCategory = arrSubCategory?.compactMap { $0.copy() } as? [TblSubCategory]

In SignupFirstStepVC, I have list of all TblSubCategory which are display in collection view. So when select any item from it, it will be selected and bIsSelected is set to true and again selection will set it 'false'.

Code in SignupFirstStepVC

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {        
    self.arrSubCategory![indexPath.item].bIsSelected = !self.arrSubCategory![indexPath.item].bIsSelected
    collectionView.reloadData()
}

The using delegate I will pass array of selected subcategory to the SignupGroupVC.

This is flow of code where I'm facing some issue.

So, when I go back to Home screen and again come to SignupStepperVC, there are again method call to fetch sub-category.

So, the issue is, in database there are only 20 record, but when I go second time in SignupStepperVC and fetch sub-categories. I'm getting 40 record. Again I'm follow above steps and go back home screen, come back to SignupStepperVC and fetch sub-categories from database getting 57 record. It's random.

So, this is continuously increasing number of record even in database only 20 record.

Please help me to fix this issue.

Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
Mayur Karmur
  • 2,119
  • 14
  • 35
  • what is nsFetchRequest()? – Alastar Apr 26 '19 at 12:17
  • It's fetch request for `TblSubCategory`. I have update my question, please check it. – Mayur Karmur Apr 26 '19 at 12:31
  • Could you explain this line **In SignupGroupVC, I'm have list of group, so when select any group, related to that group all category will display in SignupFirstStepVC.** – Prashant Tukadiya Apr 26 '19 at 12:53
  • What is role and relationship of SignupFirstStepVC with Group and stepper vc – Prashant Tukadiya Apr 26 '19 at 12:55
  • Again if `TblSubCategory` is class then you don't need to do that `self.signupFirstStepVC?.arrSubCategory = arrSubCategory?.compactMap { $0.copy() } as? [TblSubCategory]` just directly assign it. and after that You don't need delegate to received updated values (i.e when bIsSelected) ; it will be automatically updated to your group vc as well as stepper vc. – Prashant Tukadiya Apr 26 '19 at 12:59
  • for me problem seems to be in the group vc. which bridges your stepper and first step vc. **REMEMBER THAT CLASS ARE REFERENCE TYPE** – Prashant Tukadiya Apr 26 '19 at 13:01

0 Answers0