CoreData limit related entities batch size

前端 未结 1 372
太阳男子
太阳男子 2020-12-20 01:56

I ran into a really simple CoreData issue but it seems hard to solve. Writing a simple chat app where every user has many messages.

class CoreUser: NSManaged         


        
相关标签:
1条回答
  • 2020-12-20 02:35

    A fetch request always returns the objects from the managed object context. You cannot fetch "modified" objects, e.g. CoreUser objects with a "restricted" relationship to CoreMessage.

    But what you can do instead is fetch the CoreMessage objects related to the given user:

    let fetchRequest = NSFetchRequest(entityName: "CoreMessage")
    fetchRequest.predicate = NSPredicate(format: "owner.id == %@", nodeId)
    

    and now you can add a fetch limit to restrict the number of retrieved messages:

    fetchRequest.fetchLimit = 20
    

    together with a suitable sort descriptor.

    0 讨论(0)
提交回复
热议问题