总结下CoreData常用的一些查询
fetchLimit
和fetchOffset
分页查询、多字段排序:
-(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;
}