我是新手。我的应用程序具有,SQLite数据库,两个表,自定义集合视图,自定义集合视图单元格。在集合视图单元格上,该单元格具有一个按钮,可以执行添加,删除和包含操作。
添加按钮-使用“ TRUE”更新表 删除按钮-使用“ FALSE”更新表格 包含按钮-使用“ TRUE_WITH_PROFILE”更新表
首先在viewDidLoad()中,我从数据库中获取记录并重新加载集合视图以显示数据。在这里,滚动集合视图时出现内存问题。
override func viewDidLoad() {
super.viewDidLoad()
//——code.—//
profilesArray = masterDBObjprofile.retriveMsterData(fromDB: "PROFILE", withGroupName: groupname)
}
第二,当我单击特定单元格的添加按钮时,它将更新父表和相关的子表。为此,我使用for循环来检索数据,如下所示。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
self.customCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeViewCell", for: indexPath) as? HomeViewCell
self.customCell!.configureCell(imageName: self.profileImageArray[indexPath.row] as! Array<TCMasterImageDB>, cell: self.customCell!, forItemAtIndexPath: indexPath, profileDB: self.profilesArray[indexPath.row])
self.customCell!.addToCartBtn.tag = (indexPath.row)
self.customCell!.addToCartBtn.addTarget(self, action: #selector(self.myProfileCartAction), for: UIControlEvents.touchUpInside)
return customCell! // return your cell
}
func addToCartWith(indexPath : IndexPath) -> Void {
print("start add to cart")
DispatchQueue.global(qos: .background).async {
[weak self] in
guard let strongSelf = self else { return }
let masterDB = TCMasterDB()
let masterChildDB = TCMasterChildDB()
guard let selectedMasterDBObj = strongSelf.profilesArray[(indexPath.row)] as? TCMasterDB else {return}
if selectedMasterDBObj.typeString == "PROFILE"{
masterDB.addTestToCartString = "TRUE"
masterDB.updateToCart(fromMasterDB: selectedMasterDBObj.codeString, withType: selectedMasterDBObj.typeString)
//User select profile cart from the book test list
//1. Based on that need to get profile related child objest form local DB
guard let profileChildArray = masterChildDB.retriveChildData(fromDB: selectedMasterDBObj.typeString, withCode: selectedMasterDBObj.codeString) else {return}
//2. Compare existed profile child array data from tests master DB
for childObj in profileChildArray{
autoreleasepool {
guard let masterTestDBObj: TCMasterDB = masterDB.retriveMasterTestData((childObj as! TCMasterChildDB).chldCodeString) else {return}
if masterTestDBObj != nil{
if masterTestDBObj.codeString.count != 0 && masterTestDBObj != nil{
masterDB.addTestToCartString = "TRUE_WITH_PROFILE"
masterDB.updateToCart(fromMasterDB: masterTestDBObj.codeString, withType: masterTestDBObj.typeString)
}
}
}
}
}else{
masterDB.addTestToCartString = "TRUE"
masterDB.updateToCart(fromMasterDB: selectedMasterDBObj.codeString, withType: selectedMasterDBObj.typeString)
}
//Get book test data from DB - fresh / updated data
guard let tempArray = masterDB.retriveMsterData(fromDB: "PROFILE", withGroupName: groupname) else{return}
// gaurd let tempArray = self.masterDBObjprofile.retriveMsterData(fromDB: "PROFILE", withGroupName: groupname) else {return}
strongSelf.profilesArray.removeAll()
strongSelf.profilesArray = tempArray as! [TCMasterDB]
strongSelf.getCartValue()
DispatchQueue.main.async {
strongSelf.collectionView.reloadData()
}
}
print("end add to cart")
}
在重新加载集合视图之后,使用表列值将按钮标题名称更改为remove / include。
它将对每个单元重复一次,我的应用将崩溃,并在Xcode日志上显示“来自调试器的消息:由于内存不足而终止”。
我还在for循环上使用autoreleasepool,但是它不起作用。请提出解决方案。