Is there a way to quickly read a lot of data from ‘SwiftData’?

I will create an app that supports purchase records.

I used ‘SwiftData’ to implement storage, backup, and restoration functions at once.

One problem is that it takes a lot of time to read the data through SwiftData. It takes about 2 minutes to read about 800 pieces of data.

There are not many ways I know.

For servers, I understand that Paging is used to move data efficiently.

Realm does not implement paging, but supports it at high speed through Lazy Data.

What should I do with Swift Data? How can I read a lot of data efficiently??

Below is the model I created.

@Model
class StoreModel {
    var id: String = UUID().uuidString
    var name: String = "None"
    var buyCount: Int = 0
    var buy: [BuyModel]?
    var regDate: Date?

    init(name: String,
         buyCount: Int,
         buy: [BuyModel],
         regDate: Date) {
       self.id = UUID().uuidString
       self.name = name
       self.buyCount = buyCount
       self.buy = buy
       self.regDate = regDate
    }
}


@Model
class BuyModel {
    var id: String = UUID().uuidString
    var type: Int = 0
    var amount: Double = 0
    var eachPrice: Double = 0
    var store: StoreModel?

    init(type: Int,
         amount: Double,
         eachPrice: Double,
         regDate: Date = Date()) {
       self.id = UUID().uuidString
       self.type = type
       self.amount = amount
       self.eachPrice = eachPrice
    }
}

Below is how I add data.

@MainActor func addStore(name: String,
                         buyCount: Int,
                         buy: [BuyModel],
                         regDate: Date) -> AnyPublisher<Bool, Error> {
    return Future<Bool, Error> { promise in
        let store = StoreModel(name: name,
                               buyCount: buyCount,
                               buy: buy,
                               regDate: regDate)
    
        do {
            self.container.mainContext.insert(store)
            try self.container.mainContext.save()
            promise(.success(true))
        } catch {
            print("error: (error.localizedDescription)")
            promise(.failure(error))
        }
    }.eraseToAnyPublisher()
}

@MainActor func addBuy(storeId: String,
                       buy: BuyModel) -> AnyPublisher<Bool, Error> {
    return Future<Bool, Error> { promise in
        do {
            let descriptor = FetchDescriptor<StoreModel>()
            
            let stores = try self.container.mainContext.fetch(descriptor)
        
            if let store = stores.first(where: { $0.id == storeId }) {
                store.buy?.append(buy)
                try self.container.mainContext.save()
                promise(.success(true))
            } else {
                promise(.failure(NSError(domain: "", code: -1,
                                         userInfo: [NSLocalizedDescriptionKey: "Store not found"])))
            }
        } catch {
            promise(.failure(error))
        }
    }.eraseToAnyPublisher()
}

This is the code I use to bring data.

I didn’t implement logic to get Buy because there is Buy inside when I get Store.

@MainActor func getAllStore() -> AnyPublisher<[StoreModel], Error> {
    return Future<[StoreModel], Error> { promise in
        do {
            let sortDescriptor = SortDescriptor(StoreModel.regDate, order: .reverse)
        
            let descriptor = FetchDescriptor<StoreModel>(sortBy: [sortDescriptor])
        
            let store = try self.container.mainContext.fetch(descriptor)
        
            promise(.success(store))
        } catch {
            promise(.failure(error))
        }
    }.eraseToAnyPublisher()
}

@MainActor func getStore(storeId: String) -> AnyPublisher<StoreModel, any Error> {
    return Future<StoreModel, Error> { promise in
        do {
            let descriptor = FetchDescriptor<StoreModel>()
        
            let stores = try self.container.mainContext.fetch(descriptor)
        
            if let store = stores.first(where: { $0.id == storeId }) {
                promise(.success(store))
            } else {
                promise(.failure(NSError(domain: "", code: -1,
                                         userInfo: [NSLocalizedDescriptionKey: "Store not found"]))).  
            }
        } catch {
            promise(.failure(error))
        }
    }.eraseToAnyPublisher()
}

And I generated each model through the for statement to generate the test data, and I injected the data by repeating it in a random number or generating a value.

I generated up to 100 store data and quickly imported them. There’s about 800 Buy data in each store. This is very slow.

I visualized the data through UITableView.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật