Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch records between two dates using core data ios swift

I am trying to search records between two dates in swift. I'm storing the date as shown below:

     let date = NSDate()
     let dateFormatter = NSDateFormatter()
     dateFormatter.dateFormat = "yyyy'-'MM'-'dd HH':'mm':'ss"

     println(date) // prints 2015-09-26 05:22:41 +0000

     myreport.setValue(date, forKey: "date")

And below is my code to fetch the records between two dates using NSPredicate:

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"

    let startDate:NSDate = dateFormatter.dateFromString(fromdate)!
    let endDate:NSDate = dateFormatter.dateFromString(todate)!

    println(fromdate) // prints 2015-09-22
    println(todate)  // prints 2015-09-29
    println(startDate) // prints 2015-09-21 20:00:00 +0000
    println(endDate)  // prints 2015-09-28 20:00:00 +0000
    let predicate1 = NSPredicate(format: "%@ >= date AND %@ <= date", argumentArray: [startDate, endDate])

    let predicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [predicate1])
    // Set the predicate on the fetch request
    request.predicate = predicate
    request.sortDescriptors = [NSSortDescriptor(key: "report_id", ascending: false)]
    var results : NSArray = context.executeFetchRequest(request, error: nil)!

Note:

1) My fromdate and todate is in this format 2015-09-25 (as String)

2) My data type in core data for date attribute is NSDate

3) I am able to store and fetch the date from core data successfully

When I run above code Im not getting any error but the code is not searching for records between two dates. I mean im getting the empty results array.

Can any one plz tell me what im doing wrong here. Also plz let me know if any more information needed. Thanks in advance!

like image 220
user3606682 Avatar asked Sep 25 '15 11:09

user3606682


1 Answers

Finally after many modifications and testing this worked for me as shown below. If you need how im storing the nsdate in coredata plz check the question above.

    println(self.appdel.fromdate) // prints 2015-09-25
    println(self.appdel.todate) // prints 2015-09-26

    var fromdate = "\(self.appdel.fromdate) 00:00" // add hours and mins to fromdate 
    var todate = "\(self.appdel.todate) 23:59" // add hours and mins to todate

    var context : NSManagedObjectContext = appdel.managedObjectContext!
    var request = NSFetchRequest(entityName: "TblReportsP")
    request.returnsObjectsAsFaults = false
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    dateFormatter.timeZone = NSTimeZone(name: "GMT") // this line resolved me the issue of getting one day less than the selected date
    let startDate:NSDate = dateFormatter.dateFromString(fromdate)!
    let endDate:NSDate = dateFormatter.dateFromString(todate)!
    request.predicate = NSPredicate(format: "(date >= %@) AND (date <= %@)", startDate, endDate)

    request.sortDescriptors = [NSSortDescriptor(key: "report_id", ascending: false)]
    var results : NSArray = context.executeFetchRequest(request, error: nil)!

    println(results.count)
like image 141
user3606682 Avatar answered Sep 21 '22 12:09

user3606682