这里我有一个名为ClipTable的实体我在这里获取所有用户的所有剪辑相关信息,但我想获取与登录的特定用户的剪辑信息,因为我已经使用NSUserDefaults存储了他的user_id。 我的剪辑表有一个名为created_by_user的属性,它基本上是存储剪辑的特定用户的用户ID。 因此,使用NSUserDefaults中的created_by_user属性和user_id,我想检查两者是否相同。 如果两者相同,则仅获取该特定用户的剪辑记录。 下面是我只能使用NSFetchResultController显示tableView中所有用户的所有剪辑记录的代码。 但现在我想只为特定用户获取记录。 很难做到这一点。 任何帮助表示赞赏。
user_id是[NSUserDefaults standardUserDefaults] stringForKey:@“userID”]
-(void)fetch:(NSString*)catgeory :(BOOL)All{ AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; self.managedObjectContext = delegate.managedObjectContext; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"ClipTable"]; // Add Sort Descriptors [fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"modifieddate" ascending:NO]]]; //_fetchedResultsController=nil; // Initialize Fetched Results Controller self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; if (All==NO) { [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(page_categorisation == %@)",catgeory]]; } [fetchRequest setFetchBatchSize:0]; // [fetchRequest setFetchLimit:10]; // Configure Fetched Results Controller [self.fetchedResultsController setDelegate:self]; // Perform Fetch NSError *error = nil; [self.fetchedResultsController performFetch:&error]; if (error) { NSLog(@"Unable to perform fetch."); NSLog(@"%@, %@", error, error.localizedDescription); } }Here i have an entity named ClipTable i am fetching all clip related information of all users here but i want to fetch clip information with particular user who is logged in,for that i have stored his user_id using NSUserDefaults. And my clip table has a attribute named created_by_user, which is basically a userid of a particular user who stores the clip. So using this created_by_user attribute and user_id from NSUserDefaults i want to check whether both are same. And if both are same get the clip record of that particular user only. below is the code with which i am only able to show all the clip record of all the user in tableView using NSFetchResultController. But now i want to get records for the particular user only. Hard time doing this. Any help is appreciated.
user_id is [NSUserDefaults standardUserDefaults] stringForKey:@"userID"]
-(void)fetch:(NSString*)catgeory :(BOOL)All{ AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; self.managedObjectContext = delegate.managedObjectContext; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"ClipTable"]; // Add Sort Descriptors [fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"modifieddate" ascending:NO]]]; //_fetchedResultsController=nil; // Initialize Fetched Results Controller self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; if (All==NO) { [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(page_categorisation == %@)",catgeory]]; } [fetchRequest setFetchBatchSize:0]; // [fetchRequest setFetchLimit:10]; // Configure Fetched Results Controller [self.fetchedResultsController setDelegate:self]; // Perform Fetch NSError *error = nil; [self.fetchedResultsController performFetch:&error]; if (error) { NSLog(@"Unable to perform fetch."); NSLog(@"%@, %@", error, error.localizedDescription); } }最满意答案
这是一个简单的解决方案,只需使用谓词来获取登录用户的剪辑。 从currentLoggedInUser中的NSUserDefaults获取当前登录用户的值,并在下面传递。
NSString *predicateFrmt = @"created_by_user == %@ ; NSPredicate *predicate = [NSPredicate predicateWithFormat: predicateFrmt, currentLoggedInUser]; fetchRequest.predicate = predicate;对于类别,您可以使用AND
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(page_categorisation == %@ AND created_by_user == %@ )",catgeory,currentLoggedInUser]];Here is the simple solution just use predicate to fetch the clips of logged in user. Get the value of current logged in user from NSUserDefaults in currentLoggedInUser and pass it below.
NSString *predicateFrmt = @"created_by_user == %@ ; NSPredicate *predicate = [NSPredicate predicateWithFormat: predicateFrmt, currentLoggedInUser]; fetchRequest.predicate = predicate;for Category you can use AND
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(page_categorisation == %@ AND created_by_user == %@ )",catgeory,currentLoggedInUser]];更多推荐
发布评论