Home CoreData limit related entities batch size
Reply: 0

CoreData limit related entities batch size

user4749 Published in September 19, 2018, 11:02 am

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: NSManagedObject
    @NSManaged var firstname: String?
    @NSManaged var id: String?
    @NSManaged var lastname: String?
    @NSManaged var nickname: String?
    @NSManaged var messages: NSSet

class CoreMessage: NSManagedObject
    @NSManaged var body: String?
    @NSManaged var owner: CoreUser
    @NSManaged var state: String
    @NSManaged var timestamp: NSDate

The relationship is: one-to-many

Fetching user entity:

func getCoreUser(userId: String) -> CoreUser?
    var error: NSError?;
    let fetchRequest = NSFetchRequest(entityName: "CoreUser");
    let nodeId = NSString(string: userId.lowercaseString);
    let pred = NSPredicate(format: "id == %@", nodeId);
    fetchRequest.fetchLimit = 1;
    fetchRequest.predicate = pred;
    let fetchResults = self.managedObjectContext!.executeFetchRequest(fetchRequest, error: &error) as? [CoreUser];

    if let results = fetchResults
        return results.first;
    return nil;

How can I limit the related message entity (20 messages, sorted by date). The result array contains all the messages from this user.

Thx for helping.

share|improve this question

1 Answer 1

active oldest votes
up vote 5 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.408567 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO