Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2011, 02:33 PM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 33
smithms is on a distinguished road
Default UISearchBar & filterContentForSearchText

I have a core data backed app for which I am implementing a searchBar.

Search scope is on a couple of string fields and date fields. The string searches work fine but the date searches are not returning what I would expect.

If I use "==" in my Predicate and try to search on an exact date, nothing gets returned.
If I use ">=" then I do get my date however I also get everything later than my date which I don't really want.

I would like to search on exact dates. I suspect this issue has something to do with the date formatting of the core data date field vs. my searchText formatting.

The output from "moveInDateFromSearchText" looks like this: 2009-09-19 07:00:00 +0000


Code:
       // Search on moveOutDate
	}else if ([scope isEqualToString:@"Out"]){
		
		dateFormatter = [[NSDateFormatter alloc] init];
		[dateFormatter setDateFormat:@"MM-dd-yyyy"];
		NSDate *moveOutDateFromSearchText = [dateFormatter dateFromString: searchText];
		
		NSPredicate * predicate = [NSPredicate predicateWithFormat:@"moveOutDate >= %@", moveOutDateFromSearchText];
		self.filteredListContent = [[[self fetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:predicate];
	
		
		NSLog(@"moveOutDateFromSearchText = %@",moveOutDateFromSearchText);
	
	// Search All
	}else { 
           // etc.....
       {
Thanks in advance for any help.
smithms is offline   Reply With Quote
Old 02-07-2011, 03:37 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I can't help specifically with CoreData or NSPredicate, but will offer this about dates...

Using a formatter does not limit the components that an NSDate object describes. So yes, you are formatting day, month, and year. This does not mean that hours, minutes, and seconds don't exist in that date object. They do, they simply have default values. Your log result proves this. Those values should correspond to midnight, although I'm not sure where that 7 is coming from. The important part is: those values are still there.

So when you are doing a date comparison, and looking for dates that are "equal", you have to keep this in mind. If the date you are comparing to is the same day, but occurs at 12:54:32 PM, then that won't be a match.

So you'll have to figure out how to do a date range search, or possibly do a string comparison after running both date values through your formatter.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-08-2011, 12:58 AM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 33
smithms is on a distinguished road
Thumbs up

Yup. That was the problem. I modified my search criteria to use a date range with dateByAddingTimeInterval and its working fine now. I forgot that date formatter doesn't actually change the underlying data.

Thanks Brian!
smithms is offline   Reply With Quote
Reply

Bookmarks

Tags
core data, dateformatter, nsdate, nspredicate, searchbar

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 366
14 members and 352 guests
7twenty7, blasterbr, Clouds, dre, EvilElf, jeroenkeij, jimmyon122, Mah6447, Morrisone, n00b, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:48 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0