Rework outgoing message state.

// FREEBIE
This commit is contained in:
Matthew Chen 2017-04-12 10:26:47 -04:00
parent 04dc930e0c
commit 654ef89049
3 changed files with 32 additions and 8 deletions

View File

@ -86,6 +86,7 @@ NS_ASSUME_NONNULL_BEGIN
attachmentIds:[NSMutableArray new]
expiresInSeconds:transcript.expirationDuration
expireStartedAt:transcript.expirationStartedAt];
// Since updateWithWasDelivered is a new message, updateWithWasDelivered will save it.
[textMessage updateWithWasDelivered];
}
}

View File

@ -109,18 +109,30 @@ typedef NS_ENUM(NSInteger, TSOutgoingMessageState) {
filename:(nullable NSString *)filename;
// TSOutgoingMessage are updated from many threads. We don't want to save
// our local copy since it may be out of date. Instead, we use these
// "updateWith..." methods to:
// our local copy (this instance) since it may be out of date. Instead, we
// use these "updateWith..." methods to:
//
// a) Update a property of the local copy.
// a) Update a property of this instance.
// b) Load an up-to-date instance of this model from from the data store.
// c) Update and save that fresh instance.
// d) If this message hasn't yet been saved, save this local instance.
//
// After "updateWith...":
//
// a) An updated copy of this message will always have been saved in the
// data store.
// b) The local property on this instance will always have been updated.
// c) Other properties on this instance may be out of date.
//
// All mutable properties of this class have been made read-only to
// prevent accidentally modifying them directly.
//
// This isn't a perfect arrangement, but in practice this will prevent
// data loss.
// data loss and will resolve all known issues.
- (void)updateWithMessageState:(TSOutgoingMessageState)messageState;
- (void)updateWithSendingError:(NSError *)error;
- (void)updateWithHasSyncedTranscript:(BOOL)hasSyncedTranscript;
- (void)updateWithCustomMessage:(NSString *)customMessage transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithCustomMessage:(NSString *)customMessage;
- (void)updateWithWasDeliveredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithWasDelivered;

View File

@ -189,6 +189,8 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
- (void)updateWithSendingError:(NSError *)error
{
OWSAssert(error);
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatest:transaction
changeBlock:^(TSOutgoingMessage *message) {
@ -218,19 +220,28 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
}];
}
- (void)updateWithCustomMessage:(NSString *)customMessage transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(customMessage);
OWSAssert(transaction);
[self applyChangeToSelfAndLatest:transaction
changeBlock:^(TSOutgoingMessage *message) {
[message setCustomMessage:customMessage];
}];
}
- (void)updateWithCustomMessage:(NSString *)customMessage
{
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatest:transaction
changeBlock:^(TSOutgoingMessage *message) {
[message setCustomMessage:customMessage];
}];
[self updateWithCustomMessage:customMessage transaction:transaction];
}];
}
- (void)updateWithWasDeliveredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(transaction);
[self applyChangeToSelfAndLatest:transaction
changeBlock:^(TSOutgoingMessage *message) {
[message setWasDelivered:YES];