我用一个具有单个String属性的实体创建了一个简单的DataModel,然后插入了1M测试数据 NSManagedObjectContext的remainsRegisteredObjects属性默认为false
获取请求
@nonobjc public class func fetchRequestWithSort() -> NSFetchRequest<AccountMO> {
let req = NSFetchRequest<AccountMO>(entityName: "Account")
req.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
req.fetchBatchSize = 100
return req
}
SwiftUI视图
struct ListViewNative: View {
@FetchRequest(fetchRequest: AccountMO.fetchRequestWithSort()) var accounts: FetchedResults<AccountMO>
var body: some View {
List(accounts, id: \.objectID) { account in
Text(account.name)
}
}
}
运行此命令并分析仪器中的内存并开始滚动,您会发现很多内存泄漏, 如果您将NSManagedObjectContext keepsRegisteredObjects属性设置为true,则内存泄漏将消失, 但是,滚动时
有人已经经历并解决了这个问题吗?