Core data join table, has many through, fetched properties predicate

后端 未结 2 1027
有刺的猬
有刺的猬 2021-02-04 16:06

\"Xcode How do you write a Fetched Property for the Place Entity that will present an Array of Users?

2条回答
  •  情歌与酒
    2021-02-04 16:40

    I hope I did understand you correctly. You want a list of users that have checked in at a certain place?

    Then you do it the other way around. You fetch all Users that have at least one Checkin with a certain Place.

    In Core-Data terms you'll need a subquery for this.

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(checkins, $CHECKIN, $CHECKIN.event == %@).@count > 0", place];
    request.predicate = predicate;
    /* and so on */
    

    There is not much documentation about SUBQUERY. A little bit is written in the discussion of expressionForSubquery:usingIteratorVariable:predicate:.

    And the fetched property would be in the Place entity with a destination entity User and the predicate SUBQUERY(checkins, $CHECKIN, $CHECKIN.event == $FETCH_SOURCE).@count > 0

提交回复
热议问题