Skip to content

Commit

Permalink
Merge pull request #87 from twobitlabs/add-count
Browse files Browse the repository at this point in the history
Add count() method to entities and optimize Core Data implementation
  • Loading branch information
Pedro Piñera Buendía committed Dec 19, 2014
2 parents c90a935 + 6088908 commit fdcf004
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 16 deletions.
5 changes: 5 additions & 0 deletions library/Core/Protocols/SugarRecordContextProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public protocol SugarRecordContext
* Executes the finder query to return filtered values
*/
func find(finder: SugarRecordFinder) -> [AnyObject]

/**
* Count the number of entities of the given type
*/
func count(objectClass: AnyClass, predicate: NSPredicate?) -> Int
}
8 changes: 8 additions & 0 deletions library/Core/Protocols/SugarRecordObjectProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public protocol SugarRecordObjectProtocol
class func all() -> SugarRecordFinder


//MARK: - Count

/**
* Returns the count of items of the class type
*/
class func count() -> Int


//MARK: - Deletion

/**
Expand Down
20 changes: 6 additions & 14 deletions library/Core/SugarRecordFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,11 @@ public class SugarRecordFinder
*/
public func count() -> Int
{
let objects: [AnyObject]? = find()
if objects != nil {
return objects!.count
}
else {
return 0
}
var count: Int = 0
SugarRecord.operation(stackType!, closure: { (context) -> () in
count = context.count(self.objectClass!, predicate: self.predicate)
})
return count
}

/**
Expand All @@ -415,12 +413,6 @@ public class SugarRecordFinder
*/
public func count(inContext context:SugarRecordContext) -> Int
{
let objects: [AnyObject]? = find(inContext: context)
if objects != nil {
return objects!.count
}
else {
return 0
}
return context.count(self.objectClass!, predicate: self.predicate)
}
}
15 changes: 15 additions & 0 deletions library/CoreData/Base/NSManagedObject+SugarRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ extension NSManagedObject: SugarRecordObjectProtocol
return finder
}

//MARK: - Count

/**
Returns a the count of elements of this type
:returns: Int
*/
public class func count() -> Int
{
var finder: SugarRecordFinder = SugarRecordFinder()
finder.objectClass = self
finder.stackType = stackType()
return finder.count()
}

//MARK: - Deletion

/**
Expand Down
14 changes: 14 additions & 0 deletions library/CoreData/Base/SugarRecordCDContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ public class SugarRecordCDContext: SugarRecordContext
SugarRecordLogger.logLevelInfo.log("Deleted \(objects.count) objects")
}

/**
* Count the number of entities of the given type
*/
public func count(objectClass: AnyClass, predicate: NSPredicate? = nil) -> Int
{
let managedObjectClass: NSManagedObject.Type = objectClass as NSManagedObject.Type
let fetchRequest: NSFetchRequest = NSFetchRequest(entityName: managedObjectClass.modelName())
fetchRequest.predicate = predicate
var error: NSError?
var count = self.contextCD.countForFetchRequest(fetchRequest, error: &error)
SugarRecordLogger.logLevelInfo.log("Found \(count) objects in database")
return count
}


//MARK: - HELPER METHODS

Expand Down
11 changes: 11 additions & 0 deletions library/Realm/RLMObject+SugarRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ extension RLMObject: SugarRecordObjectProtocol
}


//MARK: - Count

/**
* Returns the count of items of the class type
*/
public class func count() -> Int
{
return all().count()
}


//MARK: - Deletion

/**
Expand Down
18 changes: 18 additions & 0 deletions library/Realm/SugarRecordRLMContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,22 @@ public class SugarRecordRLMContext: SugarRecordContext
}
SugarRecordLogger.logLevelInfo.log("Deleted \(objects.count) objects")
}


/**
* Count the number of entities of the given type
*/
public func count(objectClass: AnyClass, predicate: NSPredicate? = nil) -> Int
{
let objectClass: RLMObject.Type = objectClass as RLMObject.Type
var objects: RLMResults? = nil
if predicate != nil {
objects = objectClass.objectsWithPredicate(predicate)
}
else {
objects = objectClass.allObjectsInRealm(self.realmContext)
}
return Int(objects!.count)
}

}
3 changes: 2 additions & 1 deletion spec/Models/CoreDataObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class CoreDataObjectTests: XCTestCase
coreDataObject2!.city = "TestCity2"
coreDataObject2!.birth = NSDate()
let saved2: Bool = coreDataObject2!.save()
XCTAssertEqual(CoreDataObject.all().count(), 2, "The count should be equal to 2")
XCTAssertEqual(CoreDataObject.count(), 2, "The count should be equal to 2")
XCTAssertEqual(CoreDataObject.by("name", equalTo: "'Realmy2'").count(), 1, "The count should be equal to 1")
coreDataObject!.beginWriting().delete().endWriting()
coreDataObject2!.beginWriting().delete().endWriting()
}
Expand Down
3 changes: 2 additions & 1 deletion spec/Models/RealmObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ class RealmObjectTests: XCTestCase
realmObject2!.city = "TestCity2"
realmObject2!.birthday = NSDate()
let saved2: Bool = realmObject2!.save()
XCTAssertEqual(RealmObject.all().count(), 2, "The count should be equal to 2")
XCTAssertEqual(RealmObject.count(), 2, "The count should be equal to 2")
XCTAssertEqual(RealmObject.by("name", equalTo: "'Realmy2'").count(), 1, "The count should be equal to 1")
realmObject!.beginWriting().delete().endWriting()
realmObject2!.beginWriting().delete().endWriting()
}
Expand Down

0 comments on commit fdcf004

Please sign in to comment.