Skip to content

Commit

Permalink
General: Fix SwiftData crash (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
anian03 authored Oct 12, 2024
1 parent c2515e0 commit 247e665
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
2 changes: 0 additions & 2 deletions Artemis.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.3.1;
OTHER_LDFLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = de.tum.cit.ase.artemis;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -505,7 +504,6 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.3.1;
OTHER_LDFLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = de.tum.cit.ase.artemis;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ls1intum/artemis-ios-core-modules",
"state" : {
"revision" : "c5ad034195a126f616ed7e5e6b71a57472a120d4",
"version" : "14.5.0"
"revision" : "dc9ddaf88a726ed1fc454e865b21bc1b4fb0c343",
"version" : "14.5.2"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Artemis/Supporting/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.3.1</string>
<string>1.3.2</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>ITSAppUsesNonExemptEncryption</key>
Expand Down
2 changes: 1 addition & 1 deletion ArtemisKit/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/Kelvas09/EmojiPicker.git", from: "1.0.0"),
.package(url: "https://github.com/ls1intum/apollon-ios-module", .upToNextMajor(from: "1.0.2")),
.package(url: "https://github.com/ls1intum/artemis-ios-core-modules", .upToNextMajor(from: "14.5.0")),
.package(url: "https://github.com/ls1intum/artemis-ios-core-modules", .upToNextMajor(from: "14.5.2")),
.package(url: "https://github.com/mac-cain13/R.swift.git", from: "7.0.0")
],
targets: [
Expand Down
4 changes: 2 additions & 2 deletions ArtemisKit/Sources/Messages/Models/Schema/SchemaV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum SchemaV1: VersionedSchema {

@Model
final class Course {
var server: Server
var server: Server?

@Attribute(.unique)
var courseId: Int
Expand All @@ -60,7 +60,7 @@ enum SchemaV1: VersionedSchema {

@Model
final class Conversation {
var course: Course
var course: Course?

@Attribute(.unique)
var conversationId: Int
Expand Down
56 changes: 36 additions & 20 deletions ArtemisKit/Sources/Messages/Repositories/MessagesRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extension MessagesRepository {
log.verbose("begin")
try purge(host: host)
let predicate = #Predicate<CourseModel> { course in
course.server.host == host
course.server?.host == host
&& course.courseId == courseId
}
return try container.mainContext.fetch(FetchDescriptor(predicate: predicate)).first
Expand All @@ -100,9 +100,13 @@ extension MessagesRepository {
log.verbose("begin")
try purge(host: host)
let predicate = #Predicate<ConversationModel> { conversation in
conversation.course.server.host == host
&& conversation.course.courseId == courseId
&& conversation.conversationId == conversationId
if let course = conversation.course {
course.server?.host == host
&& course.courseId == courseId
&& conversation.conversationId == conversationId
} else {
false
}
}
return try container.mainContext.fetch(FetchDescriptor(predicate: predicate)).first
}
Expand All @@ -116,7 +120,7 @@ extension MessagesRepository {
log.verbose("begin")
let conversation = try fetchConversation(host: host, courseId: courseId, conversationId: conversationId)
?? insertConversation(host: host, courseId: courseId, conversationId: conversationId, messageDraft: "")
try touch(server: conversation.course.server)
try touch(server: conversation.course?.server)
let message = ConversationOfflineMessageModel(conversation: conversation, date: date, text: text)
container.mainContext.insert(message)
return message
Expand All @@ -128,9 +132,13 @@ extension MessagesRepository {
log.verbose("begin")
try purge(host: host)
let predicate = #Predicate<ConversationOfflineMessageModel> { message in
message.conversation.course.server.host == host
&& message.conversation.course.courseId == courseId
&& message.conversation.conversationId == conversationId
if let course = message.conversation.course {
course.server?.host == host
&& course.courseId == courseId
&& message.conversation.conversationId == conversationId
} else {
false
}
}
return try container.mainContext.fetch(FetchDescriptor(predicate: predicate, sortBy: [SortDescriptor(\.date)]))
}
Expand All @@ -146,7 +154,7 @@ extension MessagesRepository {
log.verbose("begin")
let conversation = try fetchConversation(host: host, courseId: courseId, conversationId: conversationId)
?? insertConversation(host: host, courseId: courseId, conversationId: conversationId, messageDraft: "")
try touch(server: conversation.course.server)
try touch(server: conversation.course?.server)
let message = MessageModel(conversation: conversation, messageId: messageId, answerMessageDraft: answerMessageDraft)
container.mainContext.insert(message)
return message
Expand All @@ -156,10 +164,14 @@ extension MessagesRepository {
log.verbose("begin")
try purge(host: host)
let predicate = #Predicate<MessageModel> { message in
message.conversation.course.server.host == host
&& message.conversation.course.courseId == courseId
&& message.conversation.conversationId == conversationId
&& message.messageId == messageId
if let course = message.conversation.course {
course.server?.host == host
&& course.courseId == courseId
&& message.conversation.conversationId == conversationId
&& message.messageId == messageId
} else {
false
}
}
return try container.mainContext.fetch(FetchDescriptor(predicate: predicate)).first
}
Expand All @@ -174,7 +186,7 @@ extension MessagesRepository {
log.verbose("begin")
let message = try fetchMessage(host: host, courseId: courseId, conversationId: conversationId, messageId: messageId)
?? insertMessage(host: host, courseId: courseId, conversationId: conversationId, messageId: messageId, answerMessageDraft: "")
try touch(server: message.conversation.course.server)
try touch(server: message.conversation.course?.server)
let answer = MessageOfflineAnswerModel(message: message, date: date, text: text)
container.mainContext.insert(answer)
return answer
Expand All @@ -186,10 +198,14 @@ extension MessagesRepository {
log.verbose("begin")
try purge(host: host)
let predicate = #Predicate<MessageOfflineAnswerModel> { answer in
answer.message.conversation.course.server.host == host
&& answer.message.conversation.course.courseId == courseId
&& answer.message.conversation.conversationId == conversationId
&& answer.message.messageId == messageId
if let course = answer.message.conversation.course {
course.server?.host == host
&& course.courseId == courseId
&& answer.message.conversation.conversationId == conversationId
&& answer.message.messageId == messageId
} else {
false
}
}
return try container.mainContext.fetch(FetchDescriptor(predicate: predicate, sortBy: [SortDescriptor(\.date)]))
}
Expand All @@ -200,9 +216,9 @@ extension MessagesRepository {

// - Cache Invalidation

func touch(server: ServerModel) throws {
func touch(server: ServerModel?) throws {
log.verbose("begin")
server.lastAccessDate = .now
server?.lastAccessDate = .now
}

func purge(host: String) throws {
Expand Down

0 comments on commit 247e665

Please sign in to comment.