Recently saw some of the things about the server, some very simple things, is just a few document specifications, in addition to the recent WWDC some of the video, feel that the software architecture (software architecture) understanding and clear some, here recorded.
Software architecture sounds a big concept, actually includes a lot of things, there is a lot of controversy. It seems to me that software architectures are best understood in small scenarios.
Question 1
We have 2 pages.
Page A: Main Page
· Page B: Details page
Demo Code 1.0.0
2 pages display a number, which should be the same number. Details will change this number, and here we find that the detail page is different from the main page number
。
Inconsistent data
Issue 1 Workaround A
The first feeling here is that the details page returns, the main page data is not refreshed, resulting in inconsistent data. So the fix this bug method is to refresh the interface when the main page appears
| 12345 |
- (void)viewWillAppear:(BOOL)animated { [superviewWillAppear:animated]; self.displayLabel.text = [[CUDataDAO selectData].data stringValue];} |
It's good to see it now. However, the number of times we call Selectdata becomes very, very much. The data is not constantly changing.
Demo Code 1.0.1
Issue 1 Workaround b
We found that since the change in the data is done on page B, when page B modifies the data, the data changes should be "notified" to page A, so we wrote a delegate
| 12345 |
@protocol CUDetailViewControllerDelegate <NSObject>- (void)detailVC:(CUDetailViewController *)vc dataChanged:(NSNumber *)data;@end |
After the data is modified on page B, the delegate is notified to page A.
| 12345678910 |
- (IBAction)changeButtonClicked:(id)sender { int value = arc4random() % 100; [CUDataDAO setData:value]; self.displayLabel.text = [@(value) stringValue]; if([self.delegate respondsToSelector:@selector(detailVC:dataChanged:)]) { [self.delegate detailVC:self dataChanged:@(value)]; }} |
To this scenario 1 has been a good solution.
Demo Code 1.0.2
Question 2
Then we added another page, C. This scene is a little bit more abstract, we've defined 3 data
· Page A's data dataa
· Page B's Data datab
· Page C of Data Datac
Dataa = Datab in question 1. in question 2 Dataa = Datab + Datac;
Issue 2 Workaround C
That is to say page C's modification, also will affect page A's data, then we also want to write a xxxxdelegate?
At this time our brains sniff out some bad taste, if come another what datad,datae, we want to write so many delegate? For many-to-one "notice" this flavor, it is natural to think of not delegate, but with Nsnotification to do. Let's take a proactive look at defining a Notificaiton
| 12345 |
NSString *const kCUDataChangedNotification = @"CUDataChangedNotification";[[NSNotificationCenter defaultCenter] postNotificationName:kCUDataChangedNotification object:nil userInfo:nil]; |
That change broadcast to listener and looks like a great idea.
Demo Code 1.0.3
Question 3
After a while, we found that the method of problem 2 has a bug, when the interface stops at page b, switch to page C, modify the data, and then return in B, the data and page a data inconsistency.
Inconsistent data
That can also be analogous to solution B, which gives the following method
Workaround D
Since the data for A and B are inconsistent, and the data for a is newer than B, then a pointer to B is retained, and then a changes, then update B is good.
| 123456789101112131415 |
- (void)handleDataChangedNotification { [self updateLabel]; [self.vc updateLabel];}// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if([segue.identifier isEqualToString:@"push"]) { CUDetailViewController *vc = [segue destinationViewController]; if([vc isKindOfClass:[CUDetailViewController class]]) { self.vc = vc; } }} |
Demo Code 1.0.4
Question 4
Page c is simply too simple, this time we want to display page A's data in page C. Because the last time we had a data inconsistency problem, this time we noticed, then how to modify it?
Workaround E
After looking at the whole app various notifications, feel very troublesome, ready to use a trickery method. can be analogous to solution a. When page C appears, refresh the data, as for what performance issues, regardless of the first fix bug.
| 12345678910 |
- (void) Viewwillappear: (BOOL) animated { [self updatelabel]; } - (void) updatelabel { &NBSP;&NBSP;&NBSP;&NBSP; int datab = [[cudatadao Selectdata].data intvalue]; &NBSP;&NBSP;&NBSP;&NBSP; int datac = [[cudatadao selectotherdata].data intvalue]; &NBSP;&NBSP;&NBSP;&NBSP; self.datalabel.text = [@ (DATAB&NBSP;+&NBSP;DATAC) stringvalue]; |
Demo Code 1.0.5
Question 5
At this point the data needs to change constantly, we added a timer in Cudatadao to simulate the data changes, the reason for the data change may be the server push some data. The client local database updates the data and needs to be displayed in pages A, B, and C.
Page C's data is inconsistent ....
Where the hell is the problem?
Come here, we need to rethink why this problem repeats itself. Software architecture is here to solve the problem. But before proposing a reasonable scheme, think of a concept first.
When we show the data in the database to the screen, or to the view, the process actually makes a copy of it. And as long as it's not by reference or pointers, the way you pass the value is
Data made a copy. And this copy process is very similar to the cache.
There are 2 types of problems that usually occur when creating a cache.
· Cache condition A: inconsistent with original data, not updated in time
· Cache case B: Duplicate cache creation
Let's use this idea to see our solution.
Workaround A
This is a very typical cache case B. The data in the database has not changed, but we have repeatedly computed the cache
Workaround b
The relationship between pages can be described in the following
Here we can faintly feel the problem, a's data changes depend on 2 places. No hurry, look back.
Workaround C
Workaround d:
Things got worse.
Workaround E
Similar to solution A, the same repeated calculation of the cache problem.
Actually, it's going to be worse.
It's still a simple model, and if Project gets big, it's going to be like this.
Each x can be a bug.
We seem to have found a problem.
In "Advanced IOS application Architecture and Patterns", call this graph information flow. Our intuition will tell us that this message should be a top-down tree or a forest, preferably a level-balanced structure, clear, and each position has a relative responsibility. Then we need to make a rule.
Before thinking about this rule, if you forget the data behind the above figure, we feel that it is similar to the memory model. Of course, the memory model is more complex. But we can draw on many "rules in memory management", such as who created and who destroyed. Again, in our information flow, we want to create the cache, who updates the cache change
The DAO database seems to be very difficult to do this thing, we introduced a new element datasource (of course he is itself a cache of DAO). Where A, B, C3 will show the data, then they should be at a level where B, C will modify the data, they will return this data to DataSource, and through the datasource to the change notice to a, B, C.
The benefits are obvious, we add another d, and we don't have any effect on the data elsewhere, and our unit Test and mock are much better written.
What's wrong with our previous thinking?
From a local point of view, our previous ideas have no problems, but the overall view of the problem is hidden. The key problem is not finding the truth and finding the real place of the problem. Finding the real place requires that we have a clear information flow or data flow in the brain. Understand the relationship between the elements in order to establish a layer of each. To be able to sit true decoupling, decoupling is not just a manager, more important is to establish a clear flow mechanism, or message mechanism, if not a set of flow, the introduction of a variety of methods, even using a variety of design patterns, the entire software is still deep coupling.
Questions
The app seems very complex to interact with
The model above, some students may also feel that this is the problem of interaction above, this interaction looks very complex, not a good design.
I cite a practical example here:
A page to create an animation, the animation contains a lot of data, the data will be in b,c or even more pages, or the background is modified. The animation itself is actually reflected in the view, and these view may not only have in a, b,c may also have partial view.
How about a single case?
Of course we can use a single case. Singleton is a devil, is a lot of abuse, this scenario with a single case, in fact, only the global variable is reasonably encapsulated in a single case, because this data, there is no reason to be a copy.
Recap
After understanding this concept, it is also easier to understand the relationships between these layers when you look at some of the server's architectures and rules. Including
Why is it necessary to specify between those layers, cannot call each other, there can be no static method.
The model between a layer, cannot have overlapping function, cannot even table query.
At which layer to invoke another service, the service must also be called through a unified interface
Software architecture covers a great many things. This is just a primer, which describes the preparation before the design. But in the actual process, our model may be more complex than I write here. The next article describes a scenario where a strategy is used to handle more complex models.
iOS Project framework