Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model Services #13

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions QamarDeen/QamarDeen/Model/Generated/Day+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,40 @@ import CoreData
@objc(Day)
public class Day: NSManagedObject {

/// USAGE:
/// used stricly for creating actions, such as pray, fast, ... etc.
/// using this function will ensure there is a day backing the action.
class func fetchOrCreate(forDate date: Date) -> Day {

let query: NSFetchRequest<Day> = Day.fetchRequest()
query.predicate = NSPredicate(format: "gregorianDate = %@", date as NSDate)

let result = try! query.execute()
if let day = result.first {
return day
}

let day = Day(context: DataManager.instance.moc)
day.gregorianDate = date as NSDate
day.hijriDate = date as NSDate

return day
}

/// USAGE:
/// convenience query for retrieving a range of days for displaying information
/// some days may have not been created, for which they will be nil
class func fetchRange(startDate: Date, endDate: Date) -> [Date:Day] {
assert(startDate <= endDate, "please ensure start date preceeds endDate")

let query: NSFetchRequest<Day> = Day.fetchRequest()
query.relationshipKeyPathsForPrefetching = ["charities", "prayers", "readings", "fast"]
query.predicate = NSPredicate(format: "gregorianDate >= %@ AND gregorianDate <= %@",
startDate as NSDate, endDate as NSDate)

var daysDict: [Date:Day] = [:]
(try! query.execute()).forEach { daysDict[$0.gregorianDate! as Date] = $0 }

return daysDict
}
}