HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Core Data model for test-taking iOS app

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
coreiosappfortestdatamodeltaking

Problem

I'm building an iOS app for test-taking and I want to be sure of my model before proceeding.

I found this post very helpful and tried to implement a simplified version for Core Data.

Here are some of my assumptions:

  • Each User can take multiple tests



  • Each User has one set of answers per Test taken



  • Each Test has one User



  • Each Question has many answers



Here is my Core Data model:

Overall, what do you think of my database schema? Is it sufficient to handle a simple test-taking app?

  • Also, are Test_Questions and User_Answers necessary?



  • I could, in theory, have a relationship directly between Test andQuestion? Test -->> Question. I would like to know which is better.



Here are my model headers for Test_Questions and User_Answers:

Test_Questions.h

@class Question, Test;

@interface Test_Questions : NSManagedObject

@property (nonatomic, retain) Test *test;
@property (nonatomic, retain) NSSet *questions;
@end

@interface Test_Questions (CoreDataGeneratedAccessors)

- (void)addQuestionsObject:(Question *)value;
- (void)removeQuestionsObject:(Question *)value;
- (void)addQuestions:(NSSet *)values;
- (void)removeQuestions:(NSSet *)values;

@end


User_Answers.h

@class Answer, User;

@interface User_Answers : NSManagedObject

@property (nonatomic, retain) User *user;
@property (nonatomic, retain) NSSet *userAnswersSet;
@end

@interface User_Answers (CoreDataGeneratedAccessors)

- (void)addUserAnswersSetObject:(Answer *)value;
- (void)removeUserAnswersSetObject:(Answer *)value;
- (void)addUserAnswersSet:(NSSet *)values;
- (void)removeUserAnswersSet:(NSSet *)values;

@end

Solution

Test_Questions and User_Answers are not necessary since they do not hold any attribution and are one-to-one relationships with Test and User, respectively. It is always suspect when you have a plural table name.

However, the model fails to support the ability of a User to take multiple Tests. To fix that, User.testTaken should be replaced by User.testsTaken and made into a one-to-many relationship with Test.

Also, it seems a bit odd that a Test can only be taken by a single User, but if that's your requirement, then so be it.

Context

StackExchange Code Review Q#64102, answer score: 2

Revisions (0)

No revisions yet.