[英]How To Match NSUserDefaults and CoreData Attribute and Get Data if both are same
在這里,我有一個名為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);
}
}
這是簡單的解決方案,僅使用謂詞即可獲取已登錄用戶的剪輯。 從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]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.