开发者

conditional fetch

I am having trouble fetching a row with specific conditions.

I have an Entity called UserAccounts that it contains four attributes: userName, passWord, userFullName, userLevel.

For login, I have to get a passWord that matches the userName. To do this I have two text fields, userNameTF and passWordTF. I have to fetch a row with an entity (I am using SQL to compare my request) that matches the userNameTF.stringValue and then check if the passWord attribute is equal to passWordTF.stringValue.

Right now I can take arrays of userName and passWord from my Core Data store separately using:

NSArray *userName = [[userAccountsArray arrangedObjects] valueForKey:@"userName"];   
NSArray *passWord = [[userAccountsArray arrangedObjects] valueForKey:@"passWord"];

but these two arrays have no relation to each other, and I can't compare the password with entered username.

I done this before by SQL in VisualStudio using开发者_C百科: select "password" from userAccounts where userName = @"username" and then comparing the result with the entered password, but I can't find a way to fetch a row with an entity, to do something with its attributes.


Remember that Core Data is an object graph persistence library; SQL is abstracted away from you, and thinking in SQL terms will often just lead to confusion.

To solve this problem, you want to create a fetch request using a predicate that specifies the user name. If the user exists, it will return an array with one user object (assuming here that names are unique). Then all you have to do is compare your password string with the password property on your user managed object to check if they're the same or not.


Well, Finally I found a way solving my problem AGAIN !

Using this code I can retrieve whole attributes of an Entity's record regarding one specific attribute's value.

NSArray *userAccounts=[userAccountsArray arrangedObjects]; 
NSArray *matchingAccounts=[userAccounts  filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K == %@",@"userName",userNameTF.stringValue]];
NSString *matchedPassWords=[[matchingAccounts valueForKey:@"passWord"]objectAtIndex:0];

So I can find the passWord related to userName that user typed in the userNameTF textfield !

Anyway, this is my way to solve the problem and I think there is more solutions out there.

Thank you all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜