CoreData常用的查询

总结下CoreData常用的一些查询

fetchLimitfetchOffset分页查询、多字段排序:

-(NSArray *)sc_conversationWithParentId:(NSString *)parentId page:(int)page{
    
    static int limit=20;
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"SCConversation"];
    request.fetchLimit=limit;
    request.fetchOffset=(page-1)*limit;
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"parentId=%@ and (content.length>0 or draft.length>0) and type in {0,1,3,5}",parentId];
    request.predicate=predicate;
    
    NSSortDescriptor *topSorter=[NSSortDescriptor sortDescriptorWithKey:@"topNo" ascending:NO];
    NSSortDescriptor *timeSorter=[NSSortDescriptor sortDescriptorWithKey:@"time" ascending:NO];

    request.sortDescriptors=@[topSorter,timeSorter];
    
    NSArray *array=[self.ctxt executeFetchRequest:request error:nil];
    if(array.count==0){
        
        return nil;
    }
    return array;
}

NSExpressionDescription对某个字段求和:

-(NSInteger)totalUnreadCountInNodeName:(NSString *)nodeName{
    
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"SCConversation"];
    
    request.resultType=NSDictionaryResultType;
    
    NSExpressionDescription *expressionDescription=[[NSExpressionDescription alloc] init];
    
    NSExpression *expression=[NSExpression expressionWithFormat:@"@sum.unreadCount"];
    
    expressionDescription.expression=expression;
    expressionDescription.expressionResultType=NSInteger64AttributeType;
    expressionDescription.name=@"total";
    
    request.propertiesToFetch=@[expressionDescription];
    
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"parentId=%@",nodeName];
    request.predicate=predicate;
    
    NSArray *array=[self.ctxt executeFetchRequest:request error:nil];
    if(!array || array.count==0){
        
        return 0;
    }
    NSDictionary *dict=[array lastObject];
    return (NSInteger)[[dict objectForKey:@"total"] integerValue];
}

带时间截的查询:

-(NSArray *)sc_messageWithConversationId:(NSString *)conversationId page:(int)page timestop:(NSDate *)timestop{
    
    if(!conversationId || conversationId.length==0){
        
        return nil;
    }
    static int messagePageCount=20;
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"SCMessage"];
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"conversationId=%@ and time<=%@",conversationId,timestop];
    request.predicate=predicate;
    
    request.fetchOffset=(page-1)*messagePageCount;
    request.fetchLimit=messagePageCount;
    
    NSSortDescriptor *sorter=[NSSortDescriptor sortDescriptorWithKey:@"time" ascending:NO];
    request.sortDescriptors=@[sorter];
    
    NSArray *array=[self.ctxt executeFetchRequest:request error:nil];
    if(array.count==0){
        
        return nil;
    }
    return array;
}

多个查询条件:

-(NSArray *)sc_messageWithConversationId:(NSString *)conversationId userId:(NSString *)userId userNo:(uint64_t)userNo{
    
    if(!conversationId || conversationId.length==0){
        
        return nil;
    }
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"SCMessage"];
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"conversationId=%@ and userId=%@ and userNo=%lld",conversationId,userId,userNo];
    request.predicate=predicate;
    NSArray *array=[self.ctxt executeFetchRequest:request error:nil];
    if(array.count==0){
        
        return nil;
    }
    return array;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,776评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,004评论 18 399
  • 这个系列主要是文档与自己的用法之间的出入的反思,以后不光需要多练,仍需要更多的反思 More Control Fl...
    __XY__阅读 2,833评论 0 0
  • 【迅迅妈妈】20171026学习力七期践行D16: 今天孩子补牙,很配合没有哭,回到家已经很晚了,所以践行不是很完...
    XX_359c阅读 1,196评论 0 0
  • 浪海无边一片白,斜阳对看明月来。 玉树银盘人人望,怎掩秋思作旧姿。
    话晨阅读 3,836评论 4 12