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.