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

Add Realm.isEmpty property #2501

Closed
astigsen opened this issue Sep 9, 2015 · 7 comments · Fixed by #2526
Closed

Add Realm.isEmpty property #2501

astigsen opened this issue Sep 9, 2015 · 7 comments · Fixed by #2526
Assignees

Comments

@astigsen
Copy link
Contributor

astigsen commented Sep 9, 2015

List and Results are missing the isEmpty() method as defined on the CollectionType Protocol.

It would also be really useful with an isEmpty() method on the Realm object, to show if it contains any objects at all.

@segiddins
Copy link
Contributor

isEmpty() has a default implementation that just checks the count

@jpsim
Copy link
Contributor

jpsim commented Sep 9, 2015

That's right. If the Swift standard library has a function that works with Realm types and reimplementing it ourselves wouldn't offer any performance improvements, we don't reimplement it.

Free function in Swift 1.2: http://swiftdoc.org/func/isEmpty/

CollectionType extension in Swift 2.0: http://swiftdoc.org/swift-2/protocol/CollectionType/#prop-isEmpty

@jpsim jpsim closed this as completed Sep 9, 2015
@astigsen
Copy link
Contributor Author

astigsen commented Sep 9, 2015 via email

@jpsim
Copy link
Contributor

jpsim commented Sep 9, 2015

Cool. We should make sure that is reflected in the API docs as well.

realm/jazzy#8 (and realm/jazzy#190, kind of) would be the appropriate way to solve this I think.

Does this also extend to the Realm?

No, since Realm doesn't conform to CollectionType. We could conceivably add a Realm.isEmpty property, but can you think of practical cases in which that would be preferable to checking if the file exists, or if the count of a particular model class is empty?

@astigsen
Copy link
Contributor Author

astigsen commented Sep 9, 2015 via email

@jpsim
Copy link
Contributor

jpsim commented Sep 9, 2015

Checking if it was empty also wouldn't capture the intention of having initial data (not just seeding data whenever the Realm is empty, even on subsequent runs).

For that, I'd recommend comparing the schema version with RLMNotVersioned: https://realm.io/docs/objc/latest/api/Classes/RLMRealm.html#//api/name/schemaVersionAtPath:error:

@astigsen
Copy link
Contributor Author

I guess there are two separate issues:

One is to be able to detect if the Realm has just been created so that you can do any initial initialization you would want to do.

The other is to detect if the Realm is empty (contains no objects). There are use cases where this would be useful, like if you have lot of Realms and want to delete all the empty ones. This is currently pretty difficult to do manually as you have to find out how to iterate over all the classes in the Realm.

I think that both aught to be supported in a way that is easy for users to discover.

@jpsim jpsim changed the title Add isEmpty() methods to Realm and RealmCollectionType Add Realm.isEmpty property Sep 13, 2015
@jpsim jpsim reopened this Sep 13, 2015
jpsim added a commit that referenced this issue Sep 13, 2015
@jpsim jpsim self-assigned this Sep 13, 2015
@jpsim jpsim removed the P2 label Sep 25, 2015
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants