I've used core data to store 10000-20000 records. if i try to save and fetch 10000 records memory and cpu consumption was huge due to that app is getting crash in iphone 6 plus and earlier devices.
Here is the saving methods:----
//InboxCoredata Saving Method i..(calling saving method ii)
func InboxSaveInCoreDataWith(array: [[String: AnyObject]])
{
_ = array.map{self.InboxCreateCollectionEntityFrom(dictionary: $0)}
do
{
try CoreDataStack.sharedInstance.persistentContainer.viewContext.save()
print("Inbox Data saved Sucessfully in Coredata ")
}
catch let error
{
print(error)
}
}
// Inbox Coredata saving method ii
func InboxCreateCollectionEntityFrom(dictionary: [String: AnyObject]) -> NSManagedObject?
{
let context = CoreDataStack.sharedInstance.persistentContainer.viewContext
if let inboxEntity = NSEntityDescription.insertNewObject(forEntityName: "InboxData", into: context) as? InboxData {
inboxEntity.fileType = dictionary["FileType"] as? String
inboxEntity.sender = dictionary["Sender"] as? String
inboxEntity.mailPath = dictionary["MailPath"] as? String
inboxEntity.fullMail = dictionary["FullMail"] as? NSObject
inboxEntity.attachmentName = dictionary["AttachmentName"] as? String
inboxEntity.size = dictionary["Size"] as! Int32
inboxEntity.date = dictionary["Date"] as? NSDate
inboxEntity.dateForSearch = dictionary["DateForSearch"] as? String
inboxEntity.inboxMail = dictionary["InboxMail"] as? String
return inboxEntity
}
return nil
}
And Here is the method for fetching:----
strongSelf.inboxDataFromCoreData(fetchLimit: 0) // Methods calling in viewdidload
//MARK: - Fetching inboxData from Coredata
func inboxDataFromCoreData(fetchLimit :Int)
{
var inboxCoredataFetch = [[String : AnyObject]]()
let context = CoreDataStack.sharedInstance.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing: InboxData.self))
do {
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
fetchRequest.fetchLimit = fetchLimit
let results = try context.fetch(fetchRequest) as! [InboxData]
print("inbox_coredata:\(results .count)")
for data in results
{
let sender = data.sender
let mailPath = data.mailPath
let fileType = data.fileType
let fullMail = data.fullMail
let attachmentName = data.attachmentName
let size = data.size
let date = data.date
let dateForsearch = data.dateForSearch
let inboxMail = data.inboxMail
inboxCoredataFetch.append(["Sender" : sender as AnyObject,"MailPath": mailPath! as AnyObject, "FileType":fileType as AnyObject, "FullMail":fullMail as AnyObject, "AttachmentName": attachmentName as AnyObject, "Size":size as AnyObject,"Date": date as AnyObject,"DateForSearch" :dateForsearch as AnyObject,"InboxMail":inboxMail as AnyObject])
}
}
catch let err as NSError {
print(err.debugDescription)
}
var sortingdata = inboxCoredataFetch as Array
let mailBoxSortDescriptor = NSSortDescriptor(key: "Date", ascending:false, selector: nil)
let dateDescriptor = NSSortDescriptor(key: "AttachmentName", ascending: true, selector: #selector(NSString.caseInsensitiveCompare))
sortingdata = ((sortingdata as NSArray).sortedArray(using: [ mailBoxSortDescriptor,dateDescriptor]) ) as! [[String : AnyObject]]
inboxTotalMailData = sortingdata
if appDataLoadingFirst == true
{
appDataLoadingFirst = false
displayTotalData = sortingdata
DispatchQueue.main.async {
self.hideActivityIndicator()
self.collectionView.reloadData()
}
}
}
Core data structure is like this:::---
i've too much confusion questions on these.
- Core data is not good for storing 20000 records??
- Do i need to refreshObject(object,mergeChnage:bool) for huge data everytime i've to refresh object as like that.
- Instruments indicates memory leak in fetching results at let results = try context.fetch(fetchrequest.....line why??
- Do i need to save and fetch data in batches will that increases app performence and memory reduce??
- Why CPU indicates 100% sometimes ??
- if i display 10000 records in collection view(data loading from arrray) causes any issue?? if yes what kind of issue??
- Need your valuable suggesstion and help to make me perfect????